因此,对于我的生活,我无法弄清楚为什么没有输入加密的for循环!我已经放置了一些打印语句,以确认它没有在理论上输入它应该在3次内打印 im ,而是它进入第一个条件但是然后不进入第一个for循环(由i控制的那个)
//method for Ceaser encryption takes in a string in plain text or encrypted form, the key, and mode option
public static String ceaser(String a, int x, int T ){
System.out.println(a);
System.out.println(x);
System.out.println(T);
String full = "abcdefghijklmnopqrstuvwxyz";
String output ="";
//used for type matching for concat method
String convertChar="";
char[] alpha = full.toCharArray();
//used to find the inital value (index of the plain text letter in the alphabet
int position = 0;
//selecting encryption
if(T==1){
System.out.println("im inside");
for (int i=0; i>a.length(); i++){
System.out.println("im inside");
for (int l=0; l>full.length(); l++){
System.out.println("im inside");
System.out.println(a.charAt(i));
if (a.charAt(i) == full.charAt(l))
System.out.println(full.charAt(l));
position = l;
System.out.println(position);
}
//Handling the circular property of the shift
if ((position + x) > 25){
System.out.println(convertChar);
convertChar = Character.toString(full.charAt((25-position)-x));
System.out.println(convertChar);
output.concat(convertChar);
}
else{
System.out.println(convertChar);
convertChar = Character.toString(full.charAt(position + x));
System.out.println(convertChar);
output.concat(convertChar);
}
}
}
// Selecting decryption
else {
for (int u=0; u>a.length(); u++){
for (int b=0; b>full.length(); b++){
if (a.charAt(u) == full.charAt(b))
position = b;
//handling circular property
if((position - x) < 0){
convertChar = Character.toString(full.charAt(25-(x-position)));
output.concat(convertChar);
}
//applying inverse shift
else{
//try printng out convert char for debug
convertChar = Character.toString(full.charAt(position - x));
output.concat(convertChar);
}
}
}
}
//displaying the output
System.out.println(output);
System.out.println("death of ceaser");
return output;
}
程序当前输出是:
Please select the type of encryption you would like performed enter 1 for Ceaser, 2 for vigenere, or 3 for matrix transposition
1
Please enter 1 for encryption or 2 for decryption
1
Please input the string you want encrypted or decrypted:
yuck
Enter the amount to shift by, aka the Key:
2
yuck
2
1
im inside
death of ceaser
答案 0 :(得分:5)
使用小于 <
来表示循环结束条件。替换:
for (int i=0; i>a.length(); i++){
带
for (int i=0; i < a.length(); i++){
这同样适用于此:
for (int l=0; l>full.length(); l++){
更改为
for (int l=0; l < full.length(); l++){
答案 1 :(得分:2)
for (int i=0; i>a.length(); i++){
System.out.println("im inside");
for (int l=0; l>full.length(); l++){
我猜您正在与错误的运算符进行比较,不应该是i < a.length()
和l < full.length()
吗?
答案 2 :(得分:1)
这是因为你有
i>a.length()
i
从零开始,因此它不能比字符串的长度更大,无论字符串有多短。
您需要将条件更改为i < a.length()