Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.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 凯撒密码移位后恢复原始单词_Java_Caesar Cipher - Fatal编程技术网

Java 凯撒密码移位后恢复原始单词

Java 凯撒密码移位后恢复原始单词,java,caesar-cipher,Java,Caesar Cipher,我写了一个凯撒密码的函数 所以,在我转换一个句子之后,我还想转换回原来的句子。 现在它只适用于一个方向,当我用自然正数移位时,但当我用负数移位时,它的值小于97个ascii小写字母 我举一个例子: word: java key = 10 output: tkfk 现在我想返回,将我的word从tkfk恢复到java key = -10 output: ja\a 而不是v它把\ 我知道它发生在f到-10之间,ascii表中的字母是“\”,我想要字母v。 我想我需要操纵这条线,但我不知道怎么做,

我写了一个凯撒密码的函数

所以,在我转换一个句子之后,我还想转换回原来的句子。 现在它只适用于一个方向,当我用自然正数移位时,但当我用负数移位时,它的值小于97个ascii小写字母

我举一个例子:

word: java
key = 10
output: tkfk
现在我想返回,将我的word从tkfk恢复到java

key = -10
output: ja\a
而不是
v
它把
\

我知道它发生在f到-10之间,ascii表中的字母是“\”,我想要字母v。 我想我需要操纵这条线,但我不知道怎么做,我有点卡住了,我不知道该怎么办

char ch = (char) (((int) text[index].charAt(i) + key-97) % 26+97)
我的方法:(有点长)

公共静态void MakeCipherText(字符串[]文本,int键){
int指数=0;
如果(键>0){
如果(文本[索引]==null | |文本[索引]。等于(“”){
System.out.println(“没有固定大写字母的句子”);
}否则{
而(text[index]!=null&&!text[index].equals(“”){//只有当数组中有句子或数组中不包含空句子时,我们才能通过循环
String chiPstr=“”;

对于(inti=0;i,正如评论所建议的那样,你应该再次检查你的代码,这也会帮助你成为一名更好的程序员

如果你检查你的else部分,那就是If部分的精确副本。这也就不足为奇了。要解码凯撒密码,你基本上要用正确的密钥重新编码

例如:

如果使用
A
=>
B
对其进行编码,或者在本例中使用
1

test--> uftu
那么我们怎样才能把uftu解码回来呢? 当我们用
B
=>
A
移动它时,或者在这种情况下用
25
移动它

uftu --> test
因此,在您的需求中,如果您将
-1
放在前面用
1
编码的文本中,您需要对其进行解码

因此,基本上我们必须找到一种方法,将
-1
映射到
25
-2
映射到
24
等等

关键功能是:

-2 % 26 => 24
-1 % 26 => 25
...
此外,您现在甚至可以输入大于26的数字,因为:

500 % 26 => 6
-500 % 26 => 20
由于2%26=>2,您甚至不需要if子句。您的代码最终如下所示:

public static void MakeCipherText(String[] text, int key) {
    int index =0;
    key = (((key % 26) + 26) % 26); // See below for explanation of this weird modulo
    if( text[index] == null || text[index].equals("")) {
        System.out.println("No sentences to fix capital letters.");
    } else {
        while(text[index] != null && !text[index].equals("")) { // only if we have sentence in array or array not contain empty sentence we go through loop
            String chiPstr = "";
            for(int i=0; i<text[index].length(); i++) {//we work in every itration on 1 sentence (1 index of str array)
                if(Character.isLowerCase(text[index].charAt(i))) {//if we have lower letter than:
                    char ch = (char) (((int) text[index].charAt(i) + key-97) % 26+97); //we put asci value + Cipher value
                    chiPstr = chiPstr + ch; //each time we add to the new sentece the result


                } else if(Character.isUpperCase(text[index].charAt(i))) {//same thing like here, but its work on uppercase letters.

                    char ch = (char) (((int) text[index].charAt(i) + key-65) % 26+65);
                    chiPstr = chiPstr + ch;
                }else {// if we have space, or other characters that is no a letter, we just put him as is in a sentence.
                    chiPstr = chiPstr + text[index].charAt(i);

                }
            }
            text[index] = chiPstr;
            index ++;
        }
    }
}

正如评论所建议的那样,你应该再次检查你的代码,这也会帮助你成为一个更好的程序员。但无论如何,你认为太复杂了

如果你检查你的else部分,那就是If部分的精确副本。这也就不足为奇了。要解码凯撒密码,你基本上要用正确的密钥重新编码

例如:

如果使用
A
=>
B
对其进行编码,或者在本例中使用
1

test--> uftu
那么我们怎样才能把uftu解码回来呢? 当我们用
B
=>
A
移动它时,或者在这种情况下用
25
移动它

uftu --> test
因此,在您的需求中,如果您将
-1
放在前面用
1
编码的文本中,您需要对其进行解码

因此,基本上我们必须找到一种方法,将
-1
映射到
25
-2
映射到
24
等等

关键功能是:

-2 % 26 => 24
-1 % 26 => 25
...
此外,您现在甚至可以输入大于26的数字,因为:

500 % 26 => 6
-500 % 26 => 20
由于2%26=>2,您甚至不需要if子句。您的代码最终如下所示:

public static void MakeCipherText(String[] text, int key) {
    int index =0;
    key = (((key % 26) + 26) % 26); // See below for explanation of this weird modulo
    if( text[index] == null || text[index].equals("")) {
        System.out.println("No sentences to fix capital letters.");
    } else {
        while(text[index] != null && !text[index].equals("")) { // only if we have sentence in array or array not contain empty sentence we go through loop
            String chiPstr = "";
            for(int i=0; i<text[index].length(); i++) {//we work in every itration on 1 sentence (1 index of str array)
                if(Character.isLowerCase(text[index].charAt(i))) {//if we have lower letter than:
                    char ch = (char) (((int) text[index].charAt(i) + key-97) % 26+97); //we put asci value + Cipher value
                    chiPstr = chiPstr + ch; //each time we add to the new sentece the result


                } else if(Character.isUpperCase(text[index].charAt(i))) {//same thing like here, but its work on uppercase letters.

                    char ch = (char) (((int) text[index].charAt(i) + key-65) % 26+65);
                    chiPstr = chiPstr + ch;
                }else {// if we have space, or other characters that is no a letter, we just put him as is in a sentence.
                    chiPstr = chiPstr + text[index].charAt(i);

                }
            }
            text[index] = chiPstr;
            index ++;
        }
    }
}

糟糕的英语使您无法使用shift/caps lock?请尽您所能设置问题的格式。请将代码拆分为方法(有趣的是,对于Java编码标准,这些方法不应以大写字符开头)。这样就更容易发现任何问题。例如,使用一种方法将一个字符转换为基于零的
int
索引并返回。我明白了。但像这样的问题很多,基本上是关于学习如何将问题分为多个部分,然后分别调试。显然存在问题如果你的解密文本低于零,但你找不到它,因为你的所有语句都由许多部分组成。你有很多重复的逻辑。你没有看到大多数if和else几乎都是完全相同的副本吗?你应该将这些部分移到其他方法。如果你简化代码,你将能够很容易地调试和发现错误r、 糟糕的英语使您无法使用shift/caps lock?请尽您所能设置问题的格式。请将代码拆分为方法(有趣的是,对于Java编码标准,这些方法不应以大写字符开头)。这样就更容易发现任何问题。例如,使用一种方法将一个字符转换为基于零的
int
索引并返回。我明白了。但像这样的问题很多,基本上是关于学习如何将问题分为多个部分,然后分别调试。显然存在问题如果你的解密文本低于零,但你找不到它,因为你的所有语句都由许多部分组成。你有很多重复的逻辑。你没有看到大多数if和else几乎都是完全相同的副本吗?你应该将这些部分移到其他方法。如果你简化代码,你将能够很容易地调试和发现错误R