这就是我目前所拥有的。它应该找到以明文输入的字母,将其与字母表中的字母匹配(区分大小写),然后根据输入的奇数键或偶数键的数字进行移动,我稍后将实现这一点。但目前,这是我得到的输出。
what would you like to encrypt?
abcdefg
enter oddkey
1
Now enter the evenkey
1
B
BB
BBB
BBBB
BBBBB
BBBBBB
BBBBBBB
这是代码的开头
String PlainText = "", cipherText = "";
String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz- ";
System.out.println("what would you like to encrypt?");
PlainText = input.nextLine();
System.out.println("enter oddkey");
oddkey = input.nextInt();
System.out.println("Now enter the evenkey");
evenkey = input.nextInt();
for ( int i = 0; i < PlainText.length(); i ++)
{for (int m = 1; m < alpha.length(); m ++ )
{if (alpha.charAt(m-1) == PlainText.charAt(i));
cipherText = cipherText + alpha.charAt(m - 1 + evenkey);
System.out.println(cipherText);
}}
那么我做错了什么,它只是重复 BBBBBBB 而不是正确地移动字母?有什么建议吗?
最佳答案
您的问题有两个方面。
1) 您需要花一些时间正确缩进代码,以便于阅读。
2) 你的 if 条件无效(甚至无法编译)。最好在 if 之后需要一个左花括号 {
,但绝对不是分号。
我认为如果您将 if block 更改为此,它将帮助您解决问题:
for ( int i = 0; i < PlainText.length(); i ++) {
for (int m = 1; m < alpha.length(); m ++ ) {
if (alpha.charAt(m-1) == PlainText.charAt(i)){
cipherText = cipherText + alpha.charAt(m - 1 + evenkey);
System.out.println(cipherText);
}
}
}
关于java - 用java编写多表转置密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35688499/