首頁 > Java > java教程 > 主體

Java 中的嵌套 if 語句

WBOY
發布: 2024-08-30 15:23:33
原創
1006 人瀏覽過

嵌套if語句是Java中依據特定條件流動的決策語句之一。這些條件的分支是程式狀態變化的結果。也就是說,另一個 if-else 中會有一個 if-else 條件。 if、if-else、if-else-if、jump、switch-case 等是 Java 中的一些其他決策語句。現在,讓我們詳細看看 Nested-if 語句。

廣告 該類別中的熱門課程 JAVA 掌握 - 專業化 | 78 課程系列 | 15 次模擬測驗

巢狀 if 語句的語法

以下是 Java 中巢狀 if 語句的語法。

If (cond1)
{
//        Executes when the cond1 is satisfied
If (cond2)
{
//        Executes when the cond2 is satisfied
}        }
登入後複製

這裡,Cond1 是條件 1,Cond2 是條件 2。

範例:

If (A1= =A2) {
Print A1 is equal to A2
If (A1= =A3) {  Print A1, A2 and A3 are equal.  }
}
登入後複製

流程圖

下圖描述了Nested-if條件的流程圖。

Java 中的嵌套 if 語句

Java 中嵌套 if 語句的工作原理

Nested-If 的工作方式與普通的 If-else 條件類似。唯一的區別是,一個 if 條件包含在另一個 if 條件中。工作如下。

  • 如果條件1為True,則轉到if條件2。如果條件2滿足,則執行其主體;否則,else 部分將執行。
  • 如果條件 1 為 False,則執行 else 部分的主體。
  • 條件檢查完成後,退出循環。
  • 繼續執行迴圈後的語句

 if-else 條件的數量 依使用者的需求而改變。

巢狀 if 語句範例

為了詳細了解 Nested-if,讓我們來看看使用 Java 的範例。

範例#1

一個簡單的java程序,僅使用if條件來實現嵌套if條件。

//Nested-if Java program with if conditions only
public class NestedIfExample {
public static void main(String args[]) {
//declare 2 variables and store some values in it
int num1 = 23;
int num2 = 45;
//if the number 1 is 23
if( num1 == 23 ) {
//if number is 45
if( num2 == 45 ) {
System.out.print("Number 1 is :"+ num1 +" and Number 2 is :"+ num2);
} // end of if condition 2
} //end of if condition 1
} //end of main method
} //end of class
登入後複製

輸出:

Java 中的嵌套 if 語句

在此程式中,宣告了兩個變數 num1 和 num2,分別儲存兩個數字 23 和 45。在if條件中,檢查num1是否為23。如果為真,則執行嵌套的if。也就是說,也檢查另一個 if 條件,即數字 2 是否為 45。同樣,列印出一行,顯示「Number 1 is 23, and Number 2 is 45」。

範例#2

一個簡單的java程序,用於實作具有if和else條件的嵌套if條件。

//Nested-if Java program with both if and else conditions
public class NestedIfExample {
public static void main(String args[]) {
//declare 2 variables and store some values in it
int num1 = 23;
int num2 = 48;
//if the number 1 is 23
if( num1 == 23 ) {
//if number is 45
if( num2 == 45 ) {
System.out.print("Number 1 is :"+ num1 +" and Number 2 is :"+ num2);
} // end of if condition 2
else
{
System.out.print("Number 2 is not 45");
}//end of else condition 2
} //end of if condition 1
} //end of main method
} //end of class
登入後複製

輸出:

Java 中的嵌套 if 語句

在此程式中,宣告了兩個變數 num1 和 num2,分別儲存兩個數字 23 和 48。在if條件中,檢查num1是否為23。如果為真,則執行嵌套的if。也就是說,也檢查另一個 if 條件,即數字 2 是否為 45。由於事實並非如此,因此會列印一行,顯示「Number 2 is not 45」。

範例#3

一個簡單的 java 程序,用於實現接收使用者輸入的 Nested-if 條件。

//Nested-if Java program that takes input from user and checks the condition
import java.util.Scanner;
public class NestedIfExample {
public static void main(String args[]) {
//create object of scanner
Scanner <u>sc</u>= new Scanner(System.in);
System.out.print("Enter the number to be checked: ");
int num1 = sc.nextInt();
//if the number 1 is greater than or equal to 23
if( num1 >= 23 ) {
System.out.print("Number 1 is :"+ num1 +" and it is greater than 23.");
//if number is 45
if( num1 >= 45 ) {
System.out.print("Oh!! it is greater than 45 also");
} // end of if condition 2
else
{
System.out.print(" But, the number "+num1+" is less than 45");
}//end of else condition 2
} //end of if condition 1
else
{
System.out.print("The number "+num1+" is less than 23");
}//end of else condition 2
} //end of main method
} //end of class
登入後複製

輸出:

Java 中的嵌套 if 語句

在此程式中,宣告了 num1 變數。然後,請使用者輸入 num1。這裡,給出33作為輸入,在if條件中,檢查num1是否大於或等於23。如果為真,則如果執行則嵌套。也就是說,也要檢查另一個 if 條件,即數字 2 是否大於或等於 45。由於事實並非如此,因此會列印一行,顯示「數字 1 是 33,並且它大於 23。但是數字 33 小於 45」。

假設我們輸入20。輸出會是什麼?讓我們檢查一下這種情況下的流程如何。

Java 中的嵌套 if 語句

在這種情況下,由於第一個條件本身不滿足,所以 else 部分被執行。也就是說,一行將列印為「數字 20 小於 23」。

結論

巢狀 if 語句是 Java 中的決策語句,其中包含某些分支,其中一個 if 條件嵌套在另一個 if 條件中。本文檔討論了 Nested-if 的語法、工作原理和範例。

以上是Java 中的嵌套 if 語句的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!