Java 教程

Java 控制語句

面向物件程式設計

Java 內建類

Java 檔案處理

Java 錯誤和異常

Java 多執行緒

Java 同步

Java 網路

Java 集合

Java 介面

Java 資料結構

Java 集合演算法

高階 Java

Java 雜項

Java API 和框架

Java 類參考

Java 有用資源

Java - if-else 語句



Java if-else 語句

Java 中,if else 語句用於根據給定的條件執行兩個程式碼塊。一個 Java if 語句 在 if 語句的布林表示式為真時執行。if 語句後面可以跟一個可選的 else 語句,當布林表示式為假時執行。

Java 中 if-else 語句的語法

以下是 if...else 語句的語法:

if(Boolean_expression) {
   // Executes when the Boolean expression is true
}else {
   // Executes when the Boolean expression is false
}

如果布林表示式計算結果為真,則將執行 if 程式碼塊,否則將執行 else 程式碼塊。

Java 中 if-else 語句的流程圖

If Else Statement

示例:Java if else 語句

在此示例中,我們演示了 if else 語句的使用。我們建立了一個 變數 x 並將其初始化為 30。然後在 if 語句中,我們用 20 檢查 x。由於 if 語句為假,因此執行 else 塊中的語句。

public class Test {

   public static void main(String args[]) {
      int x = 30;

      if( x < 20 ) {
         System.out.print("This is if statement");
      }else {
         System.out.print("This is else statement");
      }
   }
}

輸出

This is else statement

Java if-else-if 語句

if...else if...else 語句用於根據給定的條件(布林表示式)執行多個程式碼塊。

if 語句後面可以跟一個可選的 else if...else 語句,這對於使用單個 if...else if 語句測試各種條件非常有用。

要點

使用 if-else if- else 語句時,需要注意以下幾點。

  • 一個 if 可以有零個或一個 else,並且它必須位於任何 else if 之後。

  • 一個 if 可以有零到多個 else if,並且它們必須位於 else 之前。

  • 一旦 else if 成功,就不會測試任何剩餘的 else if 或 else。

if-else-if 語句的語法

以下是 if...else if...else 語句的語法:

if(Boolean_expression 1) {
   // Executes when the Boolean expression 1 is true
}else if(Boolean_expression 2) {
   // Executes when the Boolean expression 2 is true
}else if(Boolean_expression 3) {
   // Executes when the Boolean expression 3 is true
}else {
   // Executes when the none of the above condition is true.
}

示例 1:Java if … else if … else 語句

在此示例中,我們演示了 if...else if...else 語句的使用。我們建立了一個變數 x 並將其初始化為 30。然後在 if 語句中,我們用 10 檢查 x。由於 if 語句為假,因此控制權跳轉到 else if 語句,檢查 x 的另一個值,依此類推。

public class Test {

   public static void main(String args[]) {
      int x = 30;

      if( x == 10 ) {
         System.out.print("Value of X is 10");
      }else if( x == 20 ) {
         System.out.print("Value of X is 20");
      }else if( x == 30 ) {
         System.out.print("Value of X is 30");
      }else {
         System.out.print("This is else statement");
      }
   }
}

輸出

Value of X is 30

示例 2:Java if … else if … else 語句

在此示例中,我們演示了 if...else if...else 語句的使用。我們建立了一個變數 x 並將其初始化為 30.0。然後在 if 語句中,我們用 10.0 檢查 x。由於 if 語句為假,因此控制權跳轉到 else if 語句,檢查 x 的另一個值,依此類推。

public class Test {

   public static void main(String args[]) {
      double x = 30.0;

      if( x == 10.0 ) {
         System.out.print("Value of X is 10.0");
      }else if( x == 20.0 ) {
         System.out.print("Value of X is 20.0");
      }else if( x == 30.0 ) {
         System.out.print("Value of X is 30.0");
      }else {
         System.out.print("This is else statement");
      }
   }
}

輸出

Value of X is 30.0

Java 巢狀 if-else 語句

當給定條件為真時需要檢查其他條件以進行更好的決策時,使用 巢狀 if else 語句。在巢狀 if else 語句中,您可以擁有一個 if-else 語句塊位於另一個 if(或 else)塊中。

巢狀 if-else 語句的語法

以下是巢狀 if else 語句的語法

if(condition1){    
	// code block    
	if(condition2){  
		//code block
	}    
}

示例:Java 巢狀 if else 語句

以下示例使用巢狀 if..else 語句找到三個數中的最大數。

public class Test {

    public static void main(String[] args) {

        int x = 10, y = 20, z = 30;

        if(x >= y) {
            if(x >= z)
                System.out.println(x + " is the largest.");
            else
                System.out.println(z + " is the largest.");
        } else {
            if(y >= z)
                System.out.println(y + " is the largest.");
            else
                System.out.println(z + " is the largest.");
        }
    }
}

輸出

30 is the largest.
java_decision_making.htm
廣告