<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # Java 程序:按字母順序排序字符串 > 原文: [https://beginnersbook.com/2018/10/java-program-to-sort-strings-in-an-alphabetical-order/](https://beginnersbook.com/2018/10/java-program-to-sort-strings-in-an-alphabetical-order/) 在這個 [java 教程](https://beginnersbook.com/java-tutorial-for-beginners-with-examples/)中,我們將學習如何按字母順序對字符串進行排序。 ## Java 示例:按字母順序排列字符串 在這個程序中,我們要求用戶輸入他想要輸入的字符串計數以進行排序。一旦使用`Scanner`類捕獲計數,我們已初始化輸入計數大小的`String`數組,然后運行[`for`循環](https://beginnersbook.com/2015/03/for-loop-in-java-with-example/)以捕獲用戶輸入的所有字符串。 一旦我們將所有字符串存儲在字符串數組中,我們就會比較每個字符串的第一個字母,以便按字母順序對它們進行排序。 ```java import java.util.Scanner; public class JavaExample { public static void main(String[] args) { int count; String temp; Scanner scan = new Scanner(System.in); //User will be asked to enter the count of strings System.out.print("Enter number of strings you would like to enter:"); count = scan.nextInt(); String str[] = new String[count]; Scanner scan2 = new Scanner(System.in); //User is entering the strings and they are stored in an array System.out.println("Enter the Strings one by one:"); for(int i = 0; i < count; i++) { str[i] = scan2.nextLine(); } scan.close(); scan2.close(); //Sorting the strings for (int i = 0; i < count; i++) { for (int j = i + 1; j < count; j++) { if (str[i].compareTo(str[j])>0) { temp = str[i]; str[i] = str[j]; str[j] = temp; } } } //Displaying the strings after sorting them based on alphabetical order System.out.print("Strings in Sorted Order:"); for (int i = 0; i <= count - 1; i++) { System.out.print(str[i] + ", "); } } } ``` 輸出: ![Java Program to Sort Strings in an Alphabetical Order](https://img.kancloud.cn/f2/89/f2895eac8564605a0bb6bc5140c1f5c5_1024x339.jpg) **相關 Java 示例:** 1. [Java 程序:在字符串中反轉單詞](https://beginnersbook.com/2017/09/java-program-to-reverse-words-in-a-string/) 2. [Java 程序:計算和打印學生成績](https://beginnersbook.com/2017/09/java-program-to-calculate-and-display-student-grades/) 3. [Java 程序:使用遞歸反轉字符串](https://beginnersbook.com/2017/09/java-program-to-reverse-a-string-using-recursion/) 4. [Java 程序:查找字符串中的重復字符](https://beginnersbook.com/2014/07/java-program-to-find-duplicate-characters-in-a-string/)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看