-2

因此,对于我的一生,我无法弄清楚为什么没有输入用于加密的 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
4

3 回答 3

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++){
于 2013-02-24T15:10:15.807 回答
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()andl < full.length()吗?

于 2013-02-24T15:10:36.050 回答
1

这是因为你有

i>a.length()

i从零开始,因此无论该字符串有多短,它都不能大于字符串的长度。

您需要将条件更改为i < a.length()

于 2013-02-24T15:10:49.703 回答