凯撒密码,为每个循环移动字符

Caesar cypher, shifting chars in for each loop

为什么这根本无法正常工作?

for (int i = 3; i < args.length; i++) { // repeat the core program until all arguments were processed once

            int k = Integer.parseInt(args[2]); // save given key into int variable
            char[] charArray = args[i].toCharArray(); // argument from String to charArray
            int j = 0;
            for (char c : charArray) { // iterate through the Array by using a for-each loop
                if (Character.isLetter(c)) { // ascertain that only letters are alternated
                    charArray[j] = (char) ((c + k - (int)'a')%26 +(int)'a');// shifting the respective letter by the value the key holds
                }
                j++;
            }
            System.out.println(charArray);
        }

不知何故它不起作用,我不明白为什么,为什么我不能替换

charArray[j] = (char) ((c + k - (int)'a')%26 +(int)'a');

c = (char) ((c + k - (int)'a')%26 +(int)'a');
```?

对我有用。问题可能出在 args 中。下面是我用来检查它的代码。

public class C {
public static void main (String [] args){

    Encrypt(new String[]
            {
                    "",
                    "",
                    "3",
                    "abcxyz",
                    "qwedv"
            });

}

public static void Encrypt(String[] args){

    for (int i = 3; i < args.length; i++) { // repeat the core program until all arguments were processed once

        int k = Integer.parseInt(args[2]); // save given key into int variable
        char[] charArray = args[i].toCharArray(); // argument from String to charArray
        int j = 0;
        for (char c : charArray) { // iterate through the Array by using a for-each loop
            if (Character.isLetter(c)) { // ascertain that only letters are alternated
                charArray[j] = (char) ((((int)c) + k - (int)'a')%26 +(int)'a');// shifting the respective letter by the value the key holds
            }
            j++;
        }
        System.out.println(charArray);
    }


}
}

输出是,

defabc
tzhgy

编辑:

如果您正在考虑负偏移,您可以在 for 循环中添加以下检查。因此,如果新字母小于 'a',它会将字母转换为正确的字母。

if (charArray[j] < 'a'){
charArray[j] += 26;
 }

完全更新的代码如下。

public class C {
    public static void main (String [] args){

        Encrypt(new String[]
                {
                        "",
                        "",
                        "-1",
                        "abcxyz",
                        "qwedv"
                });

    }

    public static void Encrypt(String[] args){

        for (int i = 3; i < args.length; i++) { // repeat the core program until all arguments were processed once

            int k = Integer.parseInt(args[2]); // save given key into int variable



            char[] charArray = args[i].toCharArray(); // argument from String to charArray
            int j = 0;
            for (char c : charArray) { // iterate through the Array by using a for-each loop
                if (Character.isLetter(c)) { // ascertain that only letters are alternated
                    charArray[j] = (char) ((c + k - (int)'a')%26 +(int)'a');// shifting the respective letter by the value the key holds
                    if (charArray[j] < 'a'){
                        charArray[j] += 26;
                    }
                }
                j++;
            }
            System.out.println(charArray);
        }


    }
}