简体   繁体   中英

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

   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 . This will return the ASCII code of the next char.

Change to

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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