凯撒密码我的方式,行不通

时间:2022-10-15 07:27:06

I know there are bunch of topics about Caesar Cipher but I like to solve things my way. And as such it doesnt work ,but I think with some help I might get it to work my way.I am sure you all know that good feeling when you alone solve the problem.

我知道关于凯撒密码有很多话题,但我喜欢用我自己的方式来解决问题。这样做并不奏效,但我想在一些帮助下,我可以让它按我的方式运行。我相信当你独自解决问题的时候,你们都知道那种美好的感觉。

So here is my idea. To make array of chars that consist alphabet. And String with message to code. 2 for loops. one outer to set char from message, and inner that scans thru alphabet array. When letter from message meet char in array, it replaces him by 3rd (key of 3) char down in array.

这是我的想法。组成字母的字符数组。并将消息串到代码中。2循环。从消息中设置字符的外部和通过字母数组扫描的内部。当来自消息的消息在数组中遇到char时,它将在数组中以3 (key of 3) char替换他。

Here is piece of code written by now:

这是现在写的一段代码:

    char[] alphabet = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'o', 'p','r','s','t','u', 'v', 'z'};

    String message = " This is message for coding";

    message = message.toLowerCase();
    String codedMsg = ""; 


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

        for(int j =0; j < alphabet.length; j++)
        {
            if(message.charAt(i) == alphabet[j])
            {
    codedMsg += alphabet[j +3 ];

It complies well, but I receive following error when run :

它遵守良好,但我收到以下错误,当运行:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 23
        at sifra.main(sifra.java:19)

4 个解决方案

#1


2  

You're problem is that you have alphabet[j + 3]. But because j < alphabet.length, so when j = alphabet.length - 2 say alphabet[j + 3] becomes alphabet[alphabet.length + 1] so you go outside the array.

你的问题是你有字母表[j + 3]。但是因为j <字母表。长度,所以当j =字母表。长度- 2表示字母表[j + 3]变成字母表[字母表]。长度+ 1]所以你走到数组外面。< p>

To solve this you can use alphabet[(j + 3)%alphabet.length].

要解决这个问题,可以使用字母表[(j + 3)% alphabett .length]。

Now your code will run but not be correct.

现在您的代码将运行,但不是正确的。

Because you always manipulate message it will be replaced many times in the inner loop.

因为您总是操作消息,所以它将在内部循环中被多次替换。

for(int j =0; j < alphabet.length; j++)
        {
            if(message.charAt(i) == alphabet[j])
            {
                message = message.replace(message.charAt(i), alphabet[j + 3]); // THIS LINE IS THE PROBLEM

If we say that message.charAt(i) = a this will be true if(message.charAt(i) == alphabet[j]) and all a in the sting will change to d so now message.charAt(i) = d and after 3 iteration of for(int j =0; j < alphabet.length; j++) the if statement will be true again and all d in the string will be replaced with g and so on. My solution to the problem is the following but there are probably many more:

如果我们说message.charAt(i) = a,那么这将是正确的(message.charAt(i) ==字母表[j]),并且在sting中所有a都将变为d,所以现在message.charAt(i) = d和3次迭代后(int j =0;j < alphabet.length;if语句将再次为真,字符串中的所有d将被g等替换。我对这个问题的解决方法如下,但可能还有更多:

     char[] alphabet = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'o', 'p','r','s','t','u', 'v', 'z'};

        String message = " This is message for coding";

        message = message.toLowerCase();
        char[] messageArray = message.toCharArray();

        for(int i = 0; i < message.length(); i++)
        {
            for(int j =0; j < alphabet.length; j++)
            {
                if(message.charAt(i) == alphabet[j]){
                    messageArray[i] = alphabet[(j + 3)%alphabet.length];
                }
            }
        }
        System.out.println(String.copyValueOf(messageArray));

#2


2  

alphabet[j + 3] is going out of the size of the array alphabet.

字母表[j + 3]已经超出了数组字母表的大小。

#3


1  

The problem, as mentioned in comments, is that alphabet[j + 3] goes out of bounds for j greater or equal to alphabet.length - 3. The solution I can suggest is changing alphabet[j + 3] to alphabet[(j + 3) % alphabet.length], which will have a "cyclic" behaviour once the index goes out of bounds.

问题是,正如注释中提到的,字母[j + 3]超出了j大于等于的范围。长度- 3。我建议的解决办法是把字母表[j + 3]改成字母表[(j + 3) %。长度],当索引超出范围时,它将具有“循环”行为。

#4


0  

as others have pointed out, your problem is that alphabet[j+3] will be out of bounds towards the end of the alphabet. Here's a fix that gets around that.

正如其他人指出的那样,你的问题是,字母表[j+3]在接近字母表结尾时将超出界限。这里有个解决办法。

Replace this

替换这个

message = message.replace(message.charAt(i), alphabet[j + 3]);

With this

用这个

if(j < alphabet.length - 3){
    message = message.replace(message.charAt(i), alphabet[j + 3]);
}else{
message = message.replace(message.charAt(i), alphabet[j + 3 - alphabet.length]);
}

This should loop the alphabet back to the beginning if it ends up near the end. For instance if the character z is input, it outputs c

如果字母表最后接近结尾,那么应该把它循环到开头。例如,如果字符z是输入,它将输出c

#1


2  

You're problem is that you have alphabet[j + 3]. But because j < alphabet.length, so when j = alphabet.length - 2 say alphabet[j + 3] becomes alphabet[alphabet.length + 1] so you go outside the array.

你的问题是你有字母表[j + 3]。但是因为j <字母表。长度,所以当j =字母表。长度- 2表示字母表[j + 3]变成字母表[字母表]。长度+ 1]所以你走到数组外面。< p>

To solve this you can use alphabet[(j + 3)%alphabet.length].

要解决这个问题,可以使用字母表[(j + 3)% alphabett .length]。

Now your code will run but not be correct.

现在您的代码将运行,但不是正确的。

Because you always manipulate message it will be replaced many times in the inner loop.

因为您总是操作消息,所以它将在内部循环中被多次替换。

for(int j =0; j < alphabet.length; j++)
        {
            if(message.charAt(i) == alphabet[j])
            {
                message = message.replace(message.charAt(i), alphabet[j + 3]); // THIS LINE IS THE PROBLEM

If we say that message.charAt(i) = a this will be true if(message.charAt(i) == alphabet[j]) and all a in the sting will change to d so now message.charAt(i) = d and after 3 iteration of for(int j =0; j < alphabet.length; j++) the if statement will be true again and all d in the string will be replaced with g and so on. My solution to the problem is the following but there are probably many more:

如果我们说message.charAt(i) = a,那么这将是正确的(message.charAt(i) ==字母表[j]),并且在sting中所有a都将变为d,所以现在message.charAt(i) = d和3次迭代后(int j =0;j < alphabet.length;if语句将再次为真,字符串中的所有d将被g等替换。我对这个问题的解决方法如下,但可能还有更多:

     char[] alphabet = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'o', 'p','r','s','t','u', 'v', 'z'};

        String message = " This is message for coding";

        message = message.toLowerCase();
        char[] messageArray = message.toCharArray();

        for(int i = 0; i < message.length(); i++)
        {
            for(int j =0; j < alphabet.length; j++)
            {
                if(message.charAt(i) == alphabet[j]){
                    messageArray[i] = alphabet[(j + 3)%alphabet.length];
                }
            }
        }
        System.out.println(String.copyValueOf(messageArray));

#2


2  

alphabet[j + 3] is going out of the size of the array alphabet.

字母表[j + 3]已经超出了数组字母表的大小。

#3


1  

The problem, as mentioned in comments, is that alphabet[j + 3] goes out of bounds for j greater or equal to alphabet.length - 3. The solution I can suggest is changing alphabet[j + 3] to alphabet[(j + 3) % alphabet.length], which will have a "cyclic" behaviour once the index goes out of bounds.

问题是,正如注释中提到的,字母[j + 3]超出了j大于等于的范围。长度- 3。我建议的解决办法是把字母表[j + 3]改成字母表[(j + 3) %。长度],当索引超出范围时,它将具有“循环”行为。

#4


0  

as others have pointed out, your problem is that alphabet[j+3] will be out of bounds towards the end of the alphabet. Here's a fix that gets around that.

正如其他人指出的那样,你的问题是,字母表[j+3]在接近字母表结尾时将超出界限。这里有个解决办法。

Replace this

替换这个

message = message.replace(message.charAt(i), alphabet[j + 3]);

With this

用这个

if(j < alphabet.length - 3){
    message = message.replace(message.charAt(i), alphabet[j + 3]);
}else{
message = message.replace(message.charAt(i), alphabet[j + 3 - alphabet.length]);
}

This should loop the alphabet back to the beginning if it ends up near the end. For instance if the character z is input, it outputs c

如果字母表最后接近结尾,那么应该把它循环到开头。例如,如果字符z是输入,它将输出c