0
我为我的计算机科学课程介绍制作凯撒密码,并且卡住了。我已经想出了如何满足像空间这样的项目所需的一些元素,并且我已经在加密密钥设置为固定数字时工作。但是,其中一个要求是,当您点击“z”时,字母表会环绕并且用户可以输入自己的加密密钥值。还需要加密和解密消息。 任何提示任何人都可以给我我要去哪里错将不胜感激! 这是我到目前为止有:(我是做这在Eclipse)在Java中制作凯撒密码
import java.util.Scanner;
public class CaesarCipher {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("What is the message? (all lowercase)");
String plainText = keyboard.nextLine();
System.out.println("Please enter the encryption key: ");
int encryptionKey = keyboard.nextInt();
System.out.println("The encrypted text is: ");
int charPos = 0;
while (charPos < plainText.length()) {
char currChar = plainText.charAt(charPos);
int charAsNum = (int) currChar;
int cipherLetterAsNum = charAsNum + encryptionKey;
char cipherLetter = (char) cipherLetterAsNum;
if (currChar == 'x' || currChar == 'y' || currChar == 'z') {
currChar = plainText.charAt(charPos);
charAsNum = (int) currChar;
cipherLetterAsNum = charAsNum + encryptionKey - 26;
cipherLetter = (char) cipherLetterAsNum;
System.out.print(cipherLetter);
charPos = charPos + 1;
}
if (currChar == ' ') {
System.out.print(currChar);
} else {
System.out.print(cipherLetter);
}
charPos = charPos + 1;
}
}
}
a)使用输入字符来决定是否需要更改。 b)如果你需要减去26,用'<'运算符来决定。或者你可以使用'%'运算符:'cOut =(cIn - 'a'+ encryptionKey)%26 +'a'' – fabian