0

基本上,我正在制作一个凯撒密码程序,用于加密或解密用户输入的消息。但是,如果用户在消息中输入空格(例如堆栈溢出),它将对空格和字母执行加密或解密偏移过程,显然我不希望它这样做。我希望它忽略空格并将它们打印在加密或解密的消息中。

这是我的代码:

    Scanner myinput = new Scanner(System.in);   
    String plain;
    String encrypted; 
    char chars[] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z', ' ', 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z', ' ','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z', ' ','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z', ' '};

    System.out.println("Please enter your message."); 
    plain = myinput.nextLine().toUpperCase(); 
    myinput.close(); 

    char[] plaintext = plain.toCharArray(); 
    if(plain.isEmpty()) { 
            System.out.println("You need to give me a message to encrypt.");
    }

    for(int c = 0;c < plaintext.length;c++) { 
        for(int p = 0 ; p < 105;p++) { 
            if(p <= 100) { 
                if(plaintext[c] == chars[p]) {
                    plaintext[c] = chars[p + offset];
                        break;
                    }  
                }
            else if(plaintext[c] == chars[p]) {
                plaintext[c] = chars[p - 81];
            }
        }
    }
    encrypted = String.valueOf(plaintext);
    return encrypted; 
4

2 回答 2

1
for(int p = 0 ; p < 105; p++) {
    if (plaintext[p] == ' ') continue;

这就是保持空间不变所需要的全部,也可以从chars数组中删除它们

于 2015-03-10T16:17:03.527 回答
0

你的程序有一些逻辑问题,你的程序的第一个问题是,没有检查空间'',因此程序不知道找到空间时该怎么做,其次你正在添加palinttext[c]=chars[p+offset]它找到的任何内容文本将其添加到palintext[p]包括空格“”。因此,为了解决这个问题,您需要在代码中添加空格“”检查并避免空格,您需要添加另一个字符串 ( output) 来存储所需的输出,我将向您展示修改后的代码:

    Scanner myinput = new Scanner(System.in);
    String plain;
    String encrypted;
    char chars[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
            'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
            'X', 'Y', 'Z', ' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
            'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
            'U', 'V', 'W', 'X', 'Y', 'Z', ' ', 'A', 'B', 'C', 'D', 'E',
            'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
            'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', ' ', 'A', 'B',
            'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
            'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', ' ' };

    System.out.println("Please enter your message.");
    plain = myinput.nextLine().toUpperCase();
    myinput.close();

    char[] plaintext = plain.toCharArray();
    String output = ""; // adding a new string
    if (plain.isEmpty()) {
        System.out.println("You need to give me a message to encrypt.");
    }

    for (int c = 0; c < plaintext.length; c++) {
        for (int p = 0; p < 105; p++) {
            if (p <= 100) {
                if (plaintext[c] == ' ') {//checking for space
                    break;
                } else if (plaintext[c] == chars[p]) {
                    output += String.valueOf(chars[p]+offset);//storing value
                    break;
                }
            } else if (plaintext[c] == chars[p]) {
                if (plaintext[c] == ' ') {//checking for space
                    break;
                } else {
                    output += String.valueOf(chars[p]-81);//storing value
                }
            }
        }
    }
    encrypted = String.valueOf(output);
    return encrypted;
于 2015-03-11T07:27:13.483 回答