2011-12-07 94 views
2

以下是我的代碼。基本上,無論角色是按升序還是按降序排列都可以。如果輸入一個小寫字母,它會很好地工作,但是如果輸入了aB,它就會說這些字母不是inde,當它們顯然是!不是很確定,我開始對此失望!使用大小寫字符混合來轉換字符串java

text.toLowerCase(); 

    while (! text.equals("END")) 

    {  

     String string = (text); 
     char[] content = string.toCharArray(); 
     java.util.Arrays.sort(content); 
     String sorted = new String(content);   


      if (text.equals(sorted)) 
      { 
       System.out.print(text); 
       System.out.print("  letters in ascending order"); 
       System.out.println(); 

      } 

      else   
      { 
       System.out.print((text)); 
       System.out.print(" letters not in ascending order"); 
       System.out.println(); 
      } 

      System.out.println(); 
      System.out.print("#Enter input text : "); 
      text = BIO.getString(); 
     } 
    } 
} 

回答

3

您需要保存值回text(並檢查是否有小寫"end",因爲它被轉換)。

text = text.toLowerCase(); 
while (!text.equals("end")) { // ... 

toLowerCase不會修改原始字符串,而是返回一個較小的版本。

另外,如果您想保留"END"結束:

lowered = text.toLowerCase(); 
while (!text.equals("END")) { 
    // ... etc ... 
    text = BIO.getString(); 
    lowered = text.toLowerCase(); 
} 
0

爲什麼不使用text.compareToIgnoreCase(sorted) == 0

相關問題