凯撒的密码旋转与所有可能的组合



在此程序方法中,encodedecode正在工作,但我必须包括第三种方法rotation。用户必须输入一个解码单词,例如"eadaf eals",并且程序使用算法decode显示所有25个Possibiliies。

例如:

Rotation     text
1            eadaf eal  //original text from user
2            eolwk wkk
3            hello you
....
25           klsjd ddk

但是我对最后一种方法有问题。所有25个都必须有一个for循环可能性。

public class CaesarsChiffre {
    public static void main(String[] args) {
        char benutzerBefehl;
        String benutzerText;
        int rotation=0;

        System.out.println("Caesars Chiffre encode, decode, rotate.");
        System.out.println("Waehlen Sie:");
        System.out.println("V for encode");
        System.out.println("E for decode");
        System.out.println("B for rotation");
        benutzerBefehl=Input.readChar();
        switch (benutzerBefehl){ 
        case 'V':   System.out.println("Enter text to encode:");
                    benutzerText=Input.readString();
                    System.out.println("which rotation do you want?");
                    rotation=Input.readInt();
                      while (rotation < 1 || rotation > 25) {
                          System.out.println(" The key must be between 1 and 25, you entered: "+ benutzerBefehl);
                          break;
                          }
                    System.out.print("Encoded text: ");
                    System.out.println(Caesarencode(benutzerText,rotation));
        break;
        case 'E':   System.out.println("Enter text to decode:");
                    benutzerText=Input.readString();
                    System.out.println("which rotation do you want?");
                    rotation=Input.readInt();
                      while (rotation < 1 || rotation > 25) {
                          System.out.println(" The key must be between 1 and 25, you entered: "+ benutzerBefehl);
                          break;
                      }
                     System.out.println("Decoded Text :");
                     System.out.println(Caesardecode(benutzerText,rotation));
        break;
        case 'B':       System.out.println("Enter the rotated text:");
                        benutzerText=Input.readString();
                        System.out.println("Rotation        Text: ");
                        System.out.println(Caesardecode(benutzerText));
        default:
            System.out.println("Invalid option..");
        }
    }
        public static String Caesarencode(String original_text, int rot){
            int length =original_text.length();
            String cypherText="";
            for(int i=0;i<length;i++){
                char  ch=original_text.charAt(i);
                    if(Character.isLetter(ch)){
                        if(Character.isLowerCase(ch)){
                            char c=(char)(ch+rot);
                            if(c>'z'){
                                cypherText += (char)(ch-(26-rot));
                            }
                            else{
                                cypherText+=c;
                            }
                        }
                        else if(Character.isUpperCase(ch)){
                            char c=(char)(ch+rot);
                            if(c>'Z'){
                                cypherText += (char)(ch-(26-rot));
                            }
                            else{
                            cypherText += c;
                            }
                        }
                    }
                    else{
                        cypherText+=ch;
                    }
            }
            return cypherText;
        }
        public static String Caesardecode(String original_text, int rot){
            int length =original_text.length();
            String cypherText="";
            for(int i=0;i<length;i++){
                char  ch=original_text.charAt(i);
                    if(Character.isLetter(ch)){
                        if(Character.isLowerCase(ch)){
                            char c=(char)(ch-rot);
                            if(c<'a'){
                                cypherText += (char)(ch+(26-rot));
                            }
                            else{
                                cypherText+=c;
                            }
                        }
                        else if(Character.isUpperCase(ch)){
                            char c=(char)(ch-rot);
                            if(c<'A'){
                                cypherText += (char)(ch+(26-rot));
                            }
                            else{
                            cypherText += c;
                            }
                        }
                    }
                    else{
                        cypherText+=ch;
                    }
            }
            return cypherText.toString();
        }
    public static String[] Caesardecode(String secret_text){  

    String[] textNeu= new String[25];  //problems with this part
    for(int i=0;i<textNeu.length;i++){
        textNeu[i]= Caesardecode(secret_text, i)+" ";
    }
    return textNeu;
    }
}

对于您的问题,您正在打印一个数组

 System.out.println(Caesardecode(benutzerText));

您可以看到

 public static String[] Caesardecode(String secret_text){

您需要Arrays.toString(array)才能获得更可读的东西。

对于无效的选项是因为您忘记了case

末尾的break语句
case 'B':       System.out.println("Enter the rotated text:");
                benutzerText=Input.readString();
                System.out.println("Rotation        Text: ");
                System.out.println(Caesardecode(benutzerText));
    break; // ADD IT HERE
    default:
        System.out.println("Invalid option..");
    }

仅仅因为您已经做了很多事情,这里是使用一种方法来增加参数的方法。

我有点清洁代码,使用了一些您错过的Character方法(较低/上情况。

但最重要的是,%26的使用允许z 1成为a

public static String ceasar(String s, int inc){
    char[] input = s.toCharArray();
    char[] output = new char[input.length];
    for(int i = 0; i < output.length; ++i){
        char c = input[i];
        if(Character.isAlphabetic(c)){
            char a;
            if(Character.isUpperCase(c)){
                a = 'A';
            } else if(Character.isLowerCase(c)){
                a = 'a';
            } else { //not a letter, no evaluation needed
                output[i] = input[i];
                continue;
            }
            //encode the letter
            output[i] = (char)(((c - a + inc) % 26) + a);
        } else {
            output[i] = input[i];
        }
    }
    return new String(output);
}

您可以轻松地将其放入循环中

for(int i = 0; i < 26; ++i){
    ceasar("youCode", i);
}

最新更新