2011-12-14 77 views
1

我想顯示字母ether他們是升序還是不是升序,我有問題比較數組的索引。在這種情況下,我使用氣泡排序技術並修改它們。在java中按升序顯示字母

public class Main 
{ 
    public static void main(String[] args) 
    { 
     System.out.print("#Enter text : "); 
     String text = BIO.getString(); 

     while (! text.equals("END")) 
      { 
      boolean inorder = false; 
      // Convert the above string to a char array. 
      char[] arr = text.toCharArray(); 

      for (int i=0; i<arr.length-1; i++) 
      { //Check pair 

       if (arr[i] > arr[i+1]) 
       { 

        System.out.print("letters in ascending order"); 
        inorder = true; 

        } 

      } 

       if (! inorder) 
        { 
        System.out.print("letters not in ascending order"); 

        } 

       break; 

     } 

    } 
} 
+0

那麼你的問題是什麼?你的解決方案似乎正確。 – belgther 2011-12-14 14:18:51

+0

作爲一個有趣的事實,[Aegilops](http://en.wikipedia.org/wiki/Aegilops)是[最長的單詞](http://en.wikipedia.org/wiki/Longest_word_in_English#Words_with_certain_characteristics_of_notable_length)字母順序。 – JRL 2011-12-14 14:31:53

回答

5

因爲這看起來像功課,我會限制我的回答一對夫婦的提示。

你的邏輯是錯誤的。您應該按照升序排列,直到您看到相反的證據。

另外,那個print裏面的循環可以多次調用。你可能不希望這樣。

除此之外,你幾乎在那裏。

0

在您比較字符串中的所有字母之前,您現在正在判定它們是否合適。嘗試假設它們是按順序的,並且一旦找到不正確的標誌就將標誌更改爲假。

此外,您的break語句將在第一次迭代後退出while循環。