下面的代码显示了我的程序的一部分。我正在尝试为一个小挑战创建一个 atbash 加密(atbash 是一种加密方法;例如,我输入“hello”,程序将输出“svool”。它颠倒了字母顺序。更多信息:http://en .wikipedia.org/wiki/Atbash)
该程序工作......有点,当我输入“abcdef”时,程序输出“zyxwvu”。但是,假设我想输入一个实际的单词。该程序不会循环遍历数组,只会输出“test”(它不应该这样做)。我该如何解决?
char letter1 [] = new char [] {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
char letter2 [] = new char [] {'z','y','x','w','v','u','t','s','r','q','p','o','n','m','l','k','j','i','h','g','f','e','d','c','b','a'};
for (int i = 0 ; i < input.length() ; i++)
{
if(i == input.length()) {
break;
}
if (input.charAt(i) == letter1[i]) {
input.setCharAt(i, letter2[i]);
System.out.println (input);
}
else if (input.charAt(i) != letter1[i]){
System.out.println ("test");
}
}