简体   繁体   English

用户关键字替换密码循环两个不同大小的数组

[英]User keyword substitution cipher looping over two different sized arrays

Been programming for 4 weeks go easy on me... 编程4周对我来说很容易......

Aim: I need to take a user input "keyword" and use this keyword in an alphabet cipher. 目标:我需要输入用户输入“keyword”并在字母密码中使用此关键字。

Example: User provides keyword "SALT." 示例:用户提供关键字“SALT”。

Cipher: SALTBCDEFGHIJKMNOPQRUVWXYZ. 密码: SALTBCDEFGHIJKMNOPQRUVWXYZ。 (keyword + remaining alphabet letters) (关键字+剩余字母)

I have made my string keyword into a char array and I have an alphabet array. 我已将我的string关键字变为char数组,并且我有一个字母数组。 I wanted the code to loop over both arrays and if the alphabet letter was not present (equal to) the keyword letters then it would add it on to the end of the keyword. 我希望代码循环遍历两个数组,如果字母表字母不存在(等于)关键字字母,那么它会将其添加到关键字的末尾。

Issues - I am getting an ArrayOutOfBoundsException - which I believe it due to both my arrays being different sizes? 问题 - 我得到一个ArrayOutOfBoundsException - 我相信它是因为我的数组大小不同? Not sure how to fix that. 不知道如何解决这个问题。 Also would I need to create a third array to hold the keyword + remaining alphabet letters as arrays cant change size? 我还需要创建第三个数组来保存关键字+剩余的字母,因为数组不能改变大小吗? I can only use Arrays nothing else (Lists, Hashsets etcs as I've not learned them yet). 我只能使用Arrays(Lists,Hashsets等,因为我还没有学过它们)。 Thanks for any help.. 谢谢你的帮助..

    char[] arr = keyWord.toCharArray();
    for (char c:arr) {
        System.out.println(c); //printing to see if it worked   
    }

    char [] alphabet = {'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(alphabet);

    boolean flag = false;
    for (int i = 0; i <alphabet.length; i++) {
        for (int j = 0; j <keyWord.length(); j++) {

            if (alphabet[j] != arr[i]) 
            //if alpha letter is not equal to keyword letters
            flag = false;
            break;
            }
            if (flag)
            //(false) just trying to print anything so
            // i can see whats happening..
                System.out.println(alphabet[i]);

    }
}

} }

There are some variable naming problems that confuse you. 有一些变量命名问题让您感到困惑。 Replace keyWord with arr in the loop range control, since you are using arr now. 因为你现在正在使用arr ,所以在循环范围控件中用arr替换keyWord Then, you can see that arr must be indexed by j not i . 然后,你可以看到arr必须由j而不是i索引。

for (int i = 0; i <alphabet.length; i++) {
    for (int j = 0; j <arr.length(); j++) { //---------> notice me

        if (alphabet[i] != arr[j])  //---------> notice me, too
            //if alpha letter is not equal to keyword letters
            flag = false;
            break;
    }
    if (flag)
    //(false) just trying to print anything so
    // i can see whats happening..
    System.out.println(alphabet[i]);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM