Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将所有这些索引放入char数组?_Java_Encryption_Caesar Cipher - Fatal编程技术网

Java 如何将所有这些索引放入char数组?

Java 如何将所有这些索引放入char数组?,java,encryption,caesar-cipher,Java,Encryption,Caesar Cipher,我找不到一种方法可以将encryptedChar放入一个char数组,这样我就可以将消息放在一行上。如果有人能给我一个简单的解决问题的方法,或以任何方式帮助我,那将是伟大的 import java.util.*; public class MyClass { public static void main(String args[]) { Scanner sc = new Scanner(System.in); // can get the index

我找不到一种方法可以将encryptedChar放入一个char数组,这样我就可以将消息放在一行上。如果有人能给我一个简单的解决问题的方法,或以任何方式帮助我,那将是伟大的

import java.util.*;

public class MyClass {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);

        // can get the index without key. DONE
        // encrypt message. finish equation. 

        int encryptKey, encryptedIndex; 
        int plainLetterIndex = 0; 
        int loadingBar = 3; 
        char encryptedChar; 
        char[] alphaArray = {'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'};
        String redo, message;

        System.out.println("Welcome to the encrypter and decrypter program. The program will take the message");
        System.out.println("and either encrypt it or decrypt it. Depending on what you choose to do.");

        System.out.println("");

        do {
            System.out.println("Please enter your message!");
            message = sc.nextLine();
            message = message.toUpperCase();

            System.out.print("Would you like to re-enter your message? (Y/N)");
            redo = sc.nextLine();

        } while (redo.equals("Y") || redo.equals("y"));

        // declaring arrays
        char[] messageArray = message.toCharArray();
        char[] encryptedMessageArray = new char[messageArray.length];

        // asking for key
        System.out.println("Please input the 'key' you would like to use to encrypt your message.");

        while(true){
            try{
                encryptKey = Integer.parseInt(sc.nextLine());
                break;
            } catch (NumberFormatException ignore){
                System.out.println("You have entered an incorrect number, please try again!");
            }
        }


        System.out.println(" ");

        for (int i = 0; i < messageArray.length; i++) {

            //System.out.println(messageArray[i]);

            for (int k = 0; k < alphaArray.length; k++) {

                if (messageArray[i] == alphaArray[k]) {
                    plainLetterIndex = k;

                    encryptedIndex = (char)(plainLetterIndex + encryptKey) % 26;
                    encryptedChar = alphaArray[encryptedIndex];

                    //encryptedMessageArray = encryptedChar; 

                    System.out.print(encryptedChar);
                }
            }
        }
        sc.close();
    }
}
为EncryptedMessageGearray使用索引变量,每次将字符放入EncryptedMessageGearray时都增加该变量


将加密字符或原始字符添加到encryptedMessageArray中,这样您就可以解密数组并获取原始消息

for (int i = 0; i < messageArray.length; i++) {
    boolean isCharEncrypted = false;
    for (int k = 0; k < alphaArray.length; k++) {
        if (messageArray[i] == alphaArray[k]) {
            plainLetterIndex = k;

            encryptedIndex = (char)(plainLetterIndex + encryptKey) % 26;
            encryptedChar = alphaArray[encryptedIndex];

            encryptedMessageArray[i] = encryptedChar; 
            isCharEncrypted = true;
        }
    }
    if (!isCharEncrypted) {
        encryptedMessageArray[i] = messageArray[i];
    }
}
这里有一个避免内部for循环的替代解决方案

for (int i = 0; i < messageArray.length; i++) {
    char c = messageArray[i];
    if (c >= 'A' && c <= 'Z') {
       plainLetterIndex = c - 65; //A is ASCII 65

       encryptedIndex = (char) (plainLetterIndex + encryptKey) % 26;
       encryptedChar = alphaArray[encryptedIndex];

       encryptedMessageArray[i] = encryptedChar;
   } else {
       encryptedMessageArray[i] = messageArray[i];
   }
}

编辑代码,在导入java.util.*之前放置4个空格;因此,它将被放入代码块中。它只有4个字符的编辑,所以我不能为您进行编辑
for (int i = 0; i < messageArray.length; i++) {
    char c = messageArray[i];
    if (c >= 'A' && c <= 'Z') {
       plainLetterIndex = c - 65; //A is ASCII 65

       encryptedIndex = (char) (plainLetterIndex + encryptKey) % 26;
       encryptedChar = alphaArray[encryptedIndex];

       encryptedMessageArray[i] = encryptedChar;
   } else {
       encryptedMessageArray[i] = messageArray[i];
   }
}