我的替换密码没有在StringBuilder中组合加密的消息

时间:2018-02-27 03:58:08

标签: java encryption

    //Fill Each Alphabet

    System.out.println();
    for(int i = 0; i<alphabet.length; i++)
        System.out.print(i + " ");
    System.out.println();

    for(int i = 0; i <alphabet.length; i++)
    {
        alphabetOriginal[i] = i;
        alphabet[i] = i;

        char letter = (char)(alphabetOriginal[i] + 65);

        if(alphabet[i] > 9)
            System.out.print(letter+ "  ");
        else
            System.out.print(letter+ " ");
    }

    //Switch each character!
    int position, temporary;
    Random rn = new Random();

    for(int i = 0; i <alphabet.length; i++)
    {
        int j = rn.nextInt(26);

        temporary = alphabet[25-i];
        alphabet[25-i] = alphabet[j];
        alphabet[j] = temporary;
    }

    //Display the Scrambled Alphabet if they Are Interested
    if(validateAffirm(decision))
    {
        System.out.println("\n\n\tYou can see the (randomly generated) new alphabet below!\n");

        for(int i = 0; i <alphabet.length; i++)
            System.out.print(alphabet[i] + " ");

        System.out.println();

        for(int i = 0; i<alphabet.length; i++)
        {
            char ScrambleLetter = (char)(alphabet[i] + 65);

            if(alphabet[i] > 9)
                System.out.print(ScrambleLetter + "  ");
            else
                System.out.print(ScrambleLetter + " ");
        }
    }

    //Use a Binary Search to Determine the Length of Each
    StringBuilder sb4 = new StringBuilder(initMessage.length());
    int temporaryCharValue;

    for(int i = 0; i<initMessage.length(); i++)
    {
        temporaryCharValue = (int)(initMessage.charAt(i));
        temporaryCharValue-=65;
        for(int j = 0; alphabet[j] != temporaryCharValue; j++)
        {
            if(alphabet[j] == temporaryCharValue)
            {
                temporaryCharValue+=65;
                char tempChar = (char)(temporaryCharValue);
                sb4.append(tempChar);
                System.out.println(tempChar);
                sb4.toString();
            }
        }
    }
    System.out.println("\n" +sb4);

你能帮忙找出为什么我的String没有编译来确定最终的加密消息吗?数组定义得很好,但在我的嵌套for循环中,我似乎无法访问将每个翻译字符组合到最终字符串中的正确方法。我甚至不确定这个方法是否正确地翻译了人物...(这是我的第一年编程,我为任何明显的错误道歉。我是高中三年级学生)。

1 个答案:

答案 0 :(得分:0)

你的代码很难理解,有些常量我不知道它们来自哪里......但是,在我看来,你提到的循环有一个定义问题......我解释说:

根据你的算法,你的不变量(或循环条件)是list但是,内部句子(IF句子)需要alphabet[j] != temporaryCharValue,所以当alphabet[j] == temporaryCharValue为真时,循环返回(或停止),永远不会评估IF句子...