簡體   English   中英

為什么此代碼顯示ASCII值而不是字符

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

我以為將字符轉換為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);

您的錯誤是您將整數1添加到char中。 這將返回下一個字符的ASCII碼。

改成

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

您要加上+ 1,並導致將其轉換為整數,我不確定是否正確解釋了就將其刪除

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM