首页  >  文章  >  Java  >  Java 中的嵌套 if 语句

Java 中的嵌套 if 语句

WBOY
WBOY原创
2024-08-30 15:23:331008浏览

嵌套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中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn