Encoding 加上「;轮换;当我们通过旋转13来加密大写字母时 package edu.secretcode; 导入java.util.Scanner; /** *创建密码类。 * * * */ 公共类加密码{ /** *执行ROT13操作 * *@param纯文本 *要编码的文本 *@返回纯文本的rot13'd编码 */ 公共静态字符串13(字符串纯文本){ StringBuffer cryptText=新的StringBuffer(“”); 对于(int i=0;i='A')&&(currentChar 0){ System.out.println(“输入要编码的纯文本,或退出以结束”); 扫描仪键盘=新扫描仪(System.in); 字符串纯文本=keyboard.nextLine(); if(明文等于(“退出”)){ 打破 } 字符串cryptText=SecretCode.rotate13(纯文本); 字符串encodedText=SecretCode.rotate13(纯文本); System.out.println(“编码文本:“+encodedText”); } } }
我需要在一个字符上加-13,如果结果字符大于'Z',我想减去'Z',然后加'a',然后减去1(数字1,而不是字母'1'),并且只对大写字母执行此操作。我在if语句中以及在键入“HELLO WORLD!”时执行了此操作我得到了303923011009295302,我应该得到“URYYB JBEYQ!”并且该程序编码不正确。如果您有任何帮助,我们将不胜感激。请提前感谢。您在cryptText中添加的是int而不是char。请使用:Encoding 加上「;轮换;当我们通过旋转13来加密大写字母时 package edu.secretcode; 导入java.util.Scanner; /** *创建密码类。 * * * */ 公共类加密码{ /** *执行ROT13操作 * *@param纯文本 *要编码的文本 *@返回纯文本的rot13'd编码 */ 公共静态字符串13(字符串纯文本){ StringBuffer cryptText=新的StringBuffer(“”); 对于(int i=0;i='A')&&(currentChar 0){ System.out.println(“输入要编码的纯文本,或退出以结束”); 扫描仪键盘=新扫描仪(System.in); 字符串纯文本=keyboard.nextLine(); if(明文等于(“退出”)){ 打破 } 字符串cryptText=SecretCode.rotate13(纯文本); 字符串encodedText=SecretCode.rotate13(纯文本); System.out.println(“编码文本:“+encodedText”); } } },encoding,rot13,Encoding,Rot13,我需要在一个字符上加-13,如果结果字符大于'Z',我想减去'Z',然后加'a',然后减去1(数字1,而不是字母'1'),并且只对大写字母执行此操作。我在if语句中以及在键入“HELLO WORLD!”时执行了此操作我得到了303923011009295302,我应该得到“URYYB JBEYQ!”并且该程序编码不正确。如果您有任何帮助,我们将不胜感激。请提前感谢。您在cryptText中添加的是int而不是char。请使用: package edu.secretcode; import ja
package edu.secretcode;
import java.util.Scanner;
/**
* Creates the secret code class.
*
*
*
*/
public class SecretCode {
/**
* Perform the ROT13 operation
*
* @param plainText
* the text to encode
* @return the rot13'd encoding of plainText
*/
public static String rotate13(String plainText) {
StringBuffer cryptText = new StringBuffer("");
for (int i = 0; i < plainText.length() - 1; i++) {
int currentChar = plainText.charAt(i);
String cS = currentChar+"";
currentChar = (char) ((char) (currentChar - (int) 'A' + 13) % 255 + (int)'A');
if ((currentChar >= 'A') && (currentChar <= 'Z')) {
currentChar = (((currentChar - 'A')+13) % 26) + 'A' - 1;
}
else {
cryptText.append(currentChar);
}
}
return cryptText.toString();
}
/**
* Main method of the SecretCode class
*
* @param args
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (1 > 0) {
System.out.println("Enter plain text to encode, or QUIT to end");
Scanner keyboard = new Scanner(System.in);
String plainText = keyboard.nextLine();
if (plainText.equals("QUIT")) {
break;
}
String cryptText = SecretCode.rotate13(plainText);
String encodedText = SecretCode.rotate13(plainText);
System.out.println("Encoded Text: " + encodedText);
}
}
}
更新:
不必担心字符值操作的东西。你正在做各种各样的字符集假设(试着在IBMi上运行,它使用EBCDIC而不是ASCII,然后看着它崩溃)
请改用查找表:
cryptText.append ((char)currentChar);
谢谢,这是朝着正确方向迈出的一步。我认为我的if语句有问题。有什么建议吗?我很感激,但我可以让它与正确执行的if语句一起工作,有什么想法吗?谢谢你的帮助。非常感谢。int ich=(int)ch+13;if((int)'Z'
private static final String in = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static final String out = "NOPQRSTUVWXYZABCDEFGHIJKLM";
...
final int idx = in.indexOf (ch);
cryptText.append ((-1 == idx) ? ch : out.charAt (idx));