事实上,如果使用代码[A],运行失败。我不明白为什么。我也不明白,首先 - 'A'然后+'A'。为什么[A]失败? [B]好吗?什么是'A'角色?
Code [A] (char) (ch + key) % 26)
Code [B] (char) ('A' + ((ch -'A' + key) % 26))
public void run() {
setFont("Arial-PLAIN-24");
String line = readLine ("Enter line: ");
int key = readInt ("Enter key: ");
String siphertext = encryptCaesar(line , key);
println("The result is: " + siphertext);
String newplain = encryptCaesar(siphertext , -key);
println("newplain:" + newplain);
}
private String encryptCaesar(String str , int key){
if(key < 0){
key = 26 - (-key % 26);
}
String result = "";
for(int i = 0; i < str.length(); i++){
char ch = str.charAt(i);
result += encryptChar(ch,key);
}
return result;
}
/**Why can't use ((ch + key) % 26)*/
private char encryptChar(char ch, int key){
if(Character.isUpperCase(ch)){
return ((char) ('A' + ((ch -'A' + key) % 26)));
}
return ch;
}
请更清楚你想问什么。 – 2013-03-06 15:50:38
我们无法知道你为什么不能做某些事情。如果你告诉我们你是如何尝试的,以及当你尝试过时发生了什么,这会有所帮助,所以我们可以帮助你。 – Ingo 2013-03-06 16:03:20
@BheshGurung很简单。他想知道如果使用代码[A]'为什么会失败! – 2013-03-06 19:46:04