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

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Java 中的`if`和`if-else`語句 > 原文: [https://beginnersbook.com/2017/08/if-else-statement-in-java/](https://beginnersbook.com/2017/08/if-else-statement-in-java/) 當我們需要根據條件執行一組語句時,我們需要使用**控制流語句**。例如,如果一個數字大于零,那么我們要打印“正數”,但如果它小于零,那么我們要打印“負數”。在這種情況下,程序中有兩個`print`語句,但根據輸入值一次只執行一個`print`語句。我們將看到如何使用控制語句在 java 程序中編寫這種類型的條件。 在本教程中,我們將看到四種類型的控制語句,您可以根據需求在 java 程序中使用:在本教程中,我們將介紹以下條件語句: a)`if`語句 b)嵌套`if`語句 c)`if-else`語句 d)`if-else-if`語句 ## `if`語句 `if`語句包含條件,后跟語句或一組語句,如下所示: ```java if(condition){ Statement(s); } ``` 只有在給定條件為真時才會執行語句。如果條件為`false`,那么`if`語句體內的語句將被完全忽略。 ![if statement flow diagram](https://img.kancloud.cn/0d/cd/0dcd5b5d2d2409266b2edc7ebaa3b461_400x400.jpg) ### `if`語句的示例 ```java public class IfStatementExample { public static void main(String args[]){ int num=70; if( num < 100 ){ /* This println statement will only execute, * if the above condition is true */ System.out.println("number is less than 100"); } } } ``` **輸出:** ```java number is less than 100 ``` ## Java 中的嵌套`if`語句 當在另一個`if`語句中有`if`語句時,它被稱為**嵌套`if`語句**。 嵌套的結構如下所示: ```java if(condition_1) { Statement1(s); if(condition_2) { Statement2(s); } } ``` 如果`condition_1`為`true`,則執行`Statement1`。只有條件(`condition_1`和`condition_2`)都為真時,`Statement2`才會執行。 ### 嵌套`if`語句的示例 ```java public class NestedIfExample { public static void main(String args[]){ int num=70; if( num < 100 ){ System.out.println("number is less than 100"); if(num > 50){ System.out.println("number is greater than 50"); } } } } ``` **輸出:** ```java number is less than 100 number is greater than 50 ``` ## 在 Java 中使用`if-else`語句 這是`if-else`語句的外觀: ```java if(condition) { Statement(s); } else { Statement(s); } ``` 如果條件為真,則`if`內的語句將執行,如果條件為假,則`else`內的語句將執行。 ![If else flow diagram](https://img.kancloud.cn/03/13/0313a59031293c1e271bf059213e53a0_400x400.jpg) ### `if-else`語句的示例 ```java public class IfElseExample { public static void main(String args[]){ int num=120; if( num < 50 ){ System.out.println("num is less than 50"); } else { System.out.println("num is greater than or equal 50"); } } } ``` **輸出:** ```java num is greater than or equal 50 ``` ## `if-else-if`語句 當我們需要檢查多個條件時使用`if-else-if`語句。在這個聲明中,我們只有一個`if`和一個`else`,但是我們可以有多個`else if`。它也被稱為`if else if`梯子。這是它的樣子: ```java if(condition_1) { /*if condition_1 is true execute this*/ statement(s); } else if(condition_2) { /* execute this if condition_1 is not met and * condition_2 is met */ statement(s); } else if(condition_3) { /* execute this if condition_1 & condition_2 are * not met and condition_3 is met */ statement(s); } . . . else { /* if none of the condition is true * then these statements gets executed */ statement(s); } ``` **注意:**這里要注意的最重要的一點是,在`if-else-if`語句中,只要滿足條件,就會執行相應的語句集,忽略其余。如果沒有滿足條件,則執行`else`內的語句。 ### `if-else-if`的示例 ```java public class IfElseIfExample { public static void main(String args[]){ int num=1234; if(num <100 && num>=1) { System.out.println("Its a two digit number"); } else if(num <1000 && num>=100) { System.out.println("Its a three digit number"); } else if(num <10000 && num>=1000) { System.out.println("Its a four digit number"); } else if(num <100000 && num>=10000) { System.out.println("Its a five digit number"); } else { System.out.println("number is not between 1 & 99999"); } } } ``` **輸出:** ```java Its a four digit number ``` 查看這些相關的 [java 示例](https://beginnersbook.com/2017/09/java-examples/): 1. [Java 程序:使用`if..else..if`](https://beginnersbook.com/2017/09/java-program-to-find-largest-of-three-numbers/) 查找三個數字中最大的一個 2. [Java 程序:檢查數字是正數還是負數](https://beginnersbook.com/2017/09/java-program-to-check-if-number-is-positive-or-negative/) 3. [Java 程序:檢查數字是偶數還是奇數](https://beginnersbook.com/2014/02/java-program-to-check-even-or-odd-number/)
                  <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>

                              哎呀哎呀视频在线观看