2015-04-04 31 views
0

我想將字符串轉換爲大寫字母並返回。 但是我沒有收到預期的輸出。在一個簡單的java程序錯誤

class Exam4{ 
    public static void main(String args[]) throws java.io.IOException{ 
     char ch; 
     int changes = 0; 

     System.out.println("Enter a period to stop"); 

     do{ 
      ch = (char) System.in.read(); 
      if (ch >= 'a' & ch <= 'z') { 
       ch -= 32; 
       changes++; 
       System.out.println("ch"); 
      }else if (ch >= 'A' & ch <= 'Z') { 
       ch+=32; 
       changes++; 
       System.out.println("ch"); 
      } 
     }while(ch != '.'); 

     System.out.println("changes:" + changes); 
    } 
} 
+0

你的問題不是很清楚。 同樣對於將來的問題,最好將代碼複製並粘貼到帖子中,格式與代碼一樣。 至於你的錯誤,如果你想打印一個變量,你沒有雙引號。所以只是'System.out.print(ch);' – Vahx 2015-04-04 22:47:09

回答

0

你應該說

System.out.println(ch); 

打印出ch的值,而不是

System.out.println("ch"); 

你也可以說

ch = Character.toUpperCase(ch); 

的字符轉換順便說一下,大寫。

+0

真的是你的答案夥計 – Pingu 2015-04-05 11:11:34

相關問題