如何将所有这些索引放入字符数组?

我找不到一种方法可以将 encryptedChar 放入字符数组,以便我可以将消息放在一行上。如果有人能给我一个简单的问题解决方案或以任何方式帮助我,那就太好了!


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++) {


                }

            }

        }

        sc.close();

    }

}



临摹微笑
浏览 156回答 2
2回答

万千封印

使用索引变量 for encryptedMessageArray,每次将字符放入时增加它encryptedMessageArrayint index = 0;for (int i = 0; i < messageArray.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; //System.out.println(messageArray[i]);&nbsp; &nbsp; &nbsp; &nbsp; for (int k = 0; k < alphaArray.length; k++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (messageArray[i] == alphaArray[k]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; plainLetterIndex = k;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; encryptedIndex = (char)(plainLetterIndex + encryptKey) % 26;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; encryptedChar = alphaArray[encryptedIndex];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; encryptedMessageArray[index] = encryptedChar;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp;System.out.println();&nbsp;System.out.println(encryptedMessageArray);

慕运维8079593

将加密的字符或原始字符添加到您的,encryptedMessageArray以便您可以解密数组端获取原始消息for (int i = 0; i < messageArray.length; i++) {&nbsp; &nbsp; boolean isCharEncrypted = false;&nbsp; &nbsp; for (int k = 0; k < alphaArray.length; k++) {&nbsp; &nbsp; &nbsp; &nbsp; if (messageArray[i] == alphaArray[k]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; plainLetterIndex = k;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; encryptedIndex = (char)(plainLetterIndex + encryptKey) % 26;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; encryptedChar = alphaArray[encryptedIndex];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; encryptedMessageArray[i] = encryptedChar;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isCharEncrypted = true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if (!isCharEncrypted) {&nbsp; &nbsp; &nbsp; &nbsp; encryptedMessageArray[i] = messageArray[i];&nbsp; &nbsp; }}这是避免内部 for 循环的替代解决方案for (int i = 0; i < messageArray.length; i++) {&nbsp; &nbsp; char c = messageArray[i];&nbsp; &nbsp; if (c >= 'A' && c <= 'Z') {&nbsp; &nbsp; &nbsp; &nbsp;plainLetterIndex = c - 65; //A is ASCII 65&nbsp; &nbsp; &nbsp; &nbsp;encryptedIndex = (char) (plainLetterIndex + encryptKey) % 26;&nbsp; &nbsp; &nbsp; &nbsp;encryptedChar = alphaArray[encryptedIndex];&nbsp; &nbsp; &nbsp; &nbsp;encryptedMessageArray[i] = encryptedChar;&nbsp; &nbsp;} else {&nbsp; &nbsp; &nbsp; &nbsp;encryptedMessageArray[i] = messageArray[i];&nbsp; &nbsp;}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java