2011-12-07 54 views
2

以下は私のコードです。基本的には、文字が昇順か降順かに関係なく動きます。小文字の単語が入力された場合は完璧に動作しますが、たとえばaBが入力されている場合は、文字が明瞭であるときに文字がordeではないと言っています。本当にわからないし、私はこれを払拭し始めている!大文字と小文字が混在した文字列を変換する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を使用しないのはなぜ?

関連する問題