增加加密的字符值

Increasing a character value for encryption

我正在制作一个程序,通过将每个字符的值增加 5 来加密字符串。例如)A=F、B=G 等。这是我到目前为止的想法:

public class CharArray 
{
    public static void main(String[] args) 
    {   
        String str = "This sentence will be encrypted"; 

        char[] charArray = str.toCharArray(); 

        int pos=0; 

        while (pos<str.length())
        {
            char x = (charArray[pos]); 
            System.out.print((x+5) + " "); 
            //System.out.print(charArray[x]); This causes an exception error
            pos++;
        }


    }
}

但是输出结果是:

89 109 110 120 37 120 106 115 121 106 115 104 106 37 124 110 113 113 37 103 106 37 106 115 104 119 126 117 121 106 105 

别忘了施法。进行算术运算时,您的 char 被隐式​​转换为 int 因此您必须显式地将其转换回来


解决方案

System.out.println((char)(x+5));