<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>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Java `while`和`do...while`循環 > 原文: [https://www.programiz.com/java-programming/do-while-loop](https://www.programiz.com/java-programming/do-while-loop) #### 在本教程中,我們將借助示例學習如何在 Java 中使用`while`和`do while`循環,并且還將學習循環在計算機編程中的工作方式 在計算機編程中,循環用于重復特定的代碼塊,直到滿足特定條件(測試表達式為`false`)為止。 例如, 想象一下,我們需要在屏幕上打印一個句子 50 次。 好吧,我們可以通過使用`print`語句 50 次(不使用循環)來做到這一點。 您需要打印一百萬次句子怎么樣? 您需要使用循環。 使用循環,我們可以只編寫一次`print`語句,然后運行任意次數。 這只是一個簡單的示例,顯示了循環在計算機編程中的重要性。 Java 中有 3 種類型的循環:[循環](/java-programming/for-loop),`while`循環和`do-while`循環。 * * * ## Java `while`循環 Java 中`while`循環的語法為: ```java while (testExpression) { // codes inside the body of while loop } ``` ### `while`循環如何工作? 在以上語法中,括號內的**測試**表達式是布爾表達式。 如果測試表達式的計算結果為`true`, * `while`循環內的語句被執行。 * 然后,再次求值測試表達式。 該過程一直進行到測試表達式被求值為`false`為止。 如果將測試表達式求值為`false`, * `while`循環終止。 * * * ### `While`循環流程圖 ![Java while loop flowchart](https://img.kancloud.cn/91/eb/91eb956d2c6b5e6776e12d74c719fd27_320x326.png "Working of while Loop") `While`循環的原理 * * * ### 示例 1:`while`循環 ```java // Program to print line 10 times class Loop { public static void main(String[] args) { int i = 1; while (i <= 10) { System.out.println("Line " + i); ++i; } } } ``` **輸出**: ```java Line 1 Line 2 Line 3 Line 4 Line 5 Line 6 Line 7 Line 8 Line 9 Line 10 ``` 在上面的示例中,我們有一個測試表達式(`i <= 10`)。 它檢查`i`的值是否小于或等于 10。 在這里,最初`i`的值為 1。因此,測試表達式首次求值為`true`。 因此,將執行`while`循環內的`print`語句。 在`while`循環中注意以下語句 ```java ++i; ``` 該語句在每次迭代中將`i`的值增加 1。 經過 10 次迭代后,`i`的值為 11。然后,測試表達式(`i <= 10`)求值為`false`,`while`循環終止。 要了解有關測試表達式及其求值方式的更多信息,請訪問 [Java Relational Operator](/java-programming/operators#equality-relational) 和 [Java Logical Operator](/java-programming/operators#logical) 。 * * * ### 示例 2:Java `while`循環 ```java // Program to find the sum of natural numbers from 1 to 100. class AssignmentOperator { public static void main(String[] args) { int sum = 0, i = 100; while (i != 0) { sum += i; // sum = sum + i; --i; } System.out.println("Sum = " + sum); } } ``` **輸出**: ```java Sum = 5050 ``` 這里,我們有兩個變量`sum`和`i`,其初始值分別為 0 和 100。 在`while`循環的每次迭代中, * `sum`變量被分配了值:`sum + i` * `i`的值減少 1 循環繼續進行,直到`i`的值等于 0。為實現更好的可視化效果, ```java 1st iteration: i = 100, sum = 0+100 = 100, and --i = 99 2nd iteration: i = 99, sum = 100+99 = 199, and --i = 98 3rd iteration: i = 98, sum = 199+98 = 297, and --i = 97 ... .. ... ... .. ... 99th iteration: i = 2, sum = 5047+2 = 5049, and --i = 1 100th iteration: i = 1, sum = 5049+1 = 5050, and --i = 0 ``` * * * ## Java `do...while`循環 `do...while`循環類似于`while`循環,但有一個按鍵差異。 在檢查測試表達式之前,`do...while`循環的主體將執行一次。 這是`do...while`循環的語法。 ```java do { // codes inside body of do while loop } while (testExpression); ``` * * * ### `do...while`循環如何工作? `do...while`循環的主體執行一次(在檢查測試表達式之前)。 只有這樣,才檢查測試表達式。 如果將測試表達式求值為`true`,則執行循環體內的代碼,然后再次求值測試表達式。 這個過程一直進行到測試表達式被求值為`false`為止。 當測試表達式為`false`時,`do..while`循環終止。 * * * ### `do...while`循環流程圖 ![Flowchart of do while loop in Java](https://img.kancloud.cn/71/5c/715c30990d72641f697fb49cd24c0062_260x272.png "Working of do...while Loop") `do...while`循環的原理 * * * ### 示例 3:`do...while`循環 下面的程序計算用戶輸入的數字總和,直到用戶輸入 0。 為了接受用戶的輸入,我們使用了`Scanner`對象。 要了解有關`Scanner`的更多信息,請訪問 [Java Scanner](/java-programming/scanner) 。 ```java import java.util.Scanner; class Sum { public static void main(String[] args) { Double number, sum = 0.0; // creates an object of Scanner class Scanner input = new Scanner(System.in); do { // takes input from the user System.out.print("Enter a number: "); number = input.nextDouble(); sum += number; } while (number != 0.0); // test expression System.out.println("Sum = " + sum); } } ``` **輸出**: ```java Enter a number: 2.5 Enter a number: 23.3 Enter a number: -4.2 Enter a number: 3.4 Enter a number: 0 Sum = 25.0 ``` * * * ### 無限`while`循環 在使用循環時,我們應始終小心。 這是因為,如果我們錯誤地設置了測試表達式以使其永遠不會為假,則`while`和`do...while`循環將永遠運行。 這稱為無限`while`和`do...while`循環。 例如, ```java // Infinite while loop while (true) { // body of while loop } ``` 再舉一個例子 ```java // Infinite while loop int i = 100; while (i == 100) { System.out.print("Hey!"); } ``` 無限`do...while`循環的工作方式與`while`循環類似。
                  <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>

                              哎呀哎呀视频在线观看