caesar-cipher面临的挑战:编码和解码代码

时间:2016-12-30 23:35:27

标签: java netbeans caesar-cipher

我是Java新手,这就是我要做的事情:
  编写一个程序,让用户输入一个消息和一个移位值,然后输出编码的消息
编写一个单独的程序,让用户使用第一个程序输入编码消息,然后为您解码,不能使用StringBuffer或StringBuilder

我在这里要做的是制作程序的第二部分,但我遇到了一些问题。当您添加移位值时,无论您输入什么数字,它只会将其编码为1,当我尝试解码时,它会给我一个错误。

public class part2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String text;
        String key;
        int shift;

        System.out.println("Enter message:");
        text = sc.nextLine();
        System.out.println("Enter encryption key:");
        key = sc.next();
        shift = key.length(); {
            System.out.println("1.Encrypt\n2.Decrypt\n3.Exit...");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:
                    System.out.println("Encryptedmessage..." + encrypt(text, shift));
                    break;
                case 2:
                    //send retrived string from encrypt() method and keyLength to decrypt() method it returns 'Decrypted' string
                    System.out.println("Decrypted message..." + decrypt(encrypt(text, shift), shift));
                    break;
                case 3:
                    //exit from the program
                    System.exit(0);
                    break;
                default:
                    System.out.println("Invalid option..");
            }
        }
    }
    public static String encrypt(String text, int shift) {

        for (int i = 0; i < text.length(); i++) {
            char letter = text.charAt(i);

            // shift only letters (leave other characters alone)
            if (letter >= 'a' && letter <= 'z') {
                letter = (char)(letter + shift);

                // may need to wrap around
                if (letter > 'z') {
                    letter = (char)(letter - 26);
                } else if (letter < 'a') {
                    letter = (char)(letter + 26);
                }
            }
            System.out.print(letter);
        }
        return null;
    }

    public static String decrypt(String str, int keyLength) {
        String decrypted = "";
        for (int i = 0; i < str.length(); i++) {

            int c = str.charAt(i);

            if (Character.isUpperCase(c)) {
                c = c - (keyLength % 26);
                if (c < 'A')
                    c = c + 26;
            } else if (Character.isLowerCase(c)) {
                c = c - (keyLength % 26);
                if (c < 'a')
                    c = c + 26;
            }
            decrypted = decrypted + (char) c;
        }
        return decrypted;
    }
}

这是我的第一部分代码:

public static void main(String[] args) {

        Scanner console = new Scanner(System.in);
        System.out.print("Type a message you want to be coded: ");
        String message = console.nextLine();
        message = message.toLowerCase();

        System.out.print("Enter a Shift: ");
        int key = console.nextInt();
    if (key < 1 || key > 25) {
                System.out.printf(" The key must be between 1 and 25, you entered %d.\n", key);
            }
        while (key < 1 || key > 25);

        encode(message, key);
    }

    // This method encodes the given text string using a Caesar
    // cipher, shifting each letter by the given number of places.
    public static void encode(String text, int shift) {
        System.out.print("The encoded message: \n");
        for (int i = 0; i < text.length(); i++) {
            char letter = text.charAt(i);

            // shift only letters (leave other characters alone)
            if (letter >= 'a' && letter <= 'z') {
                letter = (char) (letter + shift);

                // may need to wrap around
                if (letter > 'z') {
                    letter = (char) (letter - 26);
                } else if (letter < 'a') {
                    letter = (char) (letter + 26);
                }
            }
            System.out.print(letter);
        }
    }

这是第一部分的代码,它完美无缺。只需要帮助第二部分

1 个答案:

答案 0 :(得分:0)

这里有几个问题。

首先,你并没有像你想的那样完成第一部分。我们来看看你的encrypt方法:

public static String encrypt(String text, int shift) {

    for (int i = 0; i < text.length(); i++) {
        char letter = text.charAt(i);

        // shift only letters (leave other characters alone)
        if (letter >= 'a' && letter <= 'z') {
            letter = (char)(letter + shift);

            // may need to wrap around
            if (letter > 'z') {
                letter = (char)(letter - 26);
            } else if (letter < 'a') {
                letter = (char)(letter + 26);
            }
        }
        System.out.print(letter); // <- This is part of your problem
    }
    return null; // <- This is part of your problem
}

当我们调用它时,我们将编码的字符串输出到控制台,但是看最后一行:我们返回null。您需要找出一种方法来存储编码的消息,然后将其返回。否则,您将NullPointerException方法中的main始终显示System.out.println("Decrypted message..." + decrypt(encrypt(text, shift), shift));

encrypt

第二个问题是你可能误解了你的任务。看看上面的那一行,一旦你decrypt工作,你仍然有一个无用的decrypt选项:它会一直给你回复你输入的信息。我想要做的只是ToggleBox消息,用户传入已经加密的消息。但是,这只是我的预感,你可能想与你的导师联系。