Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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_String_Cryptography_Character - Fatal编程技术网

Java中字符串的简单加密

Java中字符串的简单加密,java,string,cryptography,character,Java,String,Cryptography,Character,我想写一个程序,它将接受一个字符串,并在字符串的每个字符中添加2。这很简单。这是我的代码。 例如:- String str="ZAP YES"; nstr="BCR AGU" //note Z=B and Y=A String str=sc.nextLine(); String nstr="" for(int i=0;i<str.length();i++) { char ch=sc.charAt(i); if(ch!=' ')

我想写一个程序,它将接受一个字符串,并在字符串的每个字符中添加2。这很简单。这是我的代码。 例如:-

String str="ZAP YES";
nstr="BCR AGU" //note Z=B and Y=A

String str=sc.nextLine();
String nstr=""    
for(int i=0;i<str.length();i++)
    {
        char ch=sc.charAt(i);
        if(ch!=' ')
        {
            if(ch=='Z')
                ch='B';
            else if(ch=='Y')
                ch='A';
            else
                ch=ch+2;
        }
        nstr=nstr+ch;
    }
String str=“ZAP YES”;
nstr=“BCR AGU”//注Z=B和Y=A
字符串str=sc.nextLine();
字符串nstr=“”

对于(int i=0;i您使用
%26
的想法是正确的。缺少的一点是您的范围不是基于零的。您可以通过减去“a”(即,将“a”视为0,“B”视为1等)来模拟基于零的范围,然后读取它:

for (int i = 0; i < str.length(); i++) {
    char ch = str.charAt(i);
    if (ch != ' ') {
        ch = (char)((ch - 'A' + n) % 26 + 'A');
    }

    nstr += ch;
}
for(int i=0;i
您使用
%26
的想法是正确的。缺少的一点是您的范围不是基于零的。您可以通过减去“a”(即,将“a”视为0,“B”视为1等)然后读取它来模拟基于零的范围:

for (int i = 0; i < str.length(); i++) {
    char ch = str.charAt(i);
    if (ch != ' ') {
        ch = (char)((ch - 'A' + n) % 26 + 'A');
    }

    nstr += ch;
}
for(int i=0;i
您必须:

  • 将角色重新映射到0-26索引
  • 将增量添加到该索引中
  • 对结果应用26模
  • 将索引重新映射回ASCII
  • 例如:

    public static char increment(char c, int n) {
       return (char) (((c - 'A') + n) % 26 + 'A');
    }
    
    public static void main(String[] args) {
      System.out.println(increment('Z', 1)); // returns 'A'
      System.out.println(increment('Z', 2)); // returns 'B'
    }
    
    你必须:

  • 将角色重新映射到0-26索引
  • 将增量添加到该索引中
  • 对结果应用26模
  • 将索引重新映射回ASCII
  • 例如:

    public static char increment(char c, int n) {
       return (char) (((c - 'A') + n) % 26 + 'A');
    }
    
    public static void main(String[] args) {
      System.out.println(increment('Z', 1)); // returns 'A'
      System.out.println(increment('Z', 2)); // returns 'B'
    }
    


    考虑一下,如果有“z”,会发生什么情况?如果字符的ascii字符值为256,会发生什么情况?您的意思是“n”?n是一个变量,因为在第一种情况下,它是2,现在是在运行时决定的。请注意,可以假设输入字符串仅包含大写字母“a”到“z”之间的空格和字符。由于ASCII“Z”是90,只需测试字符是否大于90,如果是,则减去26想想如果有ch as“Z”会发生什么?如果字符的ASCII字符值为256会发生什么?您的意思是什么?'由n'表示?n是一个变量,因为在第一种情况下,它是2,现在是在运行时决定的。请注意,可能假定输入字符串仅包含空格和从大写字母“A”到“Z”的字符。因为ASCII'Z'是90,只要测试你的字符是否大于90,如果是,减去26。刚刚编译了你的代码,它工作得非常完美。谢谢。可以在2分钟内接受你的答案,并说:SOJust编译了你的代码,它工作得非常完美。谢谢。可以在2分钟内接受你的答案,并说:你迟到了一分钟,先生,我不能接受你你的答案,但它帮助了我,因此我得到了一个+1。啊哈,下次我会先发布代码,然后用注释编辑:)编码快乐!您迟到了一分钟,先生不能接受您的回答,但它帮助了我,因此我得到了a+1。啊哈,下次我将先发布代码,然后用注释进行编辑:)祝您编码愉快!