简体   繁体   English

为什么此代码显示ASCII值而不是字符

[英]Why does this code print the ASCII value instead of the character

I thought typecasting to char we do the trick but it just prints ascii values 我以为将字符转换为char是我们能做到的,但它只会打印ascii值

   String str = JOptionPane.showInputDialog("Enter a word");

   str = str.toUpperCase();
   String temp = "";

   for(int i = 0 ; i < str.length() ; i++)
   {
       temp += (char)str.charAt(i) + 1;
   }

   System.out.println(temp);

Your mistake is you are adding integer 1 to char . 您的错误是您将整数1添加到char中。 This will return the ASCII code of the next char. 这将返回下一个字符的ASCII码。

Change to 改成

temp += (char)(str.charAt(i) +1)

您要加上+ 1,并导致将其转换为整数,我不确定是否正确解释了就将其删除

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

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