Java 嵌套if else判断语句
使用嵌套的 if…else 语句是合法的。也就是说你可以在另一个 if 或者 elseif 语句中使用 if 或者 elseif 语句。
语法
if(布尔表达式 1){ 如果布尔表达式 1的值为true执行代码
if(布尔表达式 2){ 如果布尔表达式 2的值为true执行代码
}
}
也可以像 if 语句一样嵌套 else if...else。
例子
public class Test {
public static void main(String[] args) {
int x = 30;
int y = 10;
if (x == 30) {
if (y == 10) {
System.out.print("X = 30 and Y = 10");
}
}
}
}
代码编译运行结果如下:
X = 30 and Y = 10
版权声明:本文为JAVASCHOOL原创文章,未经本站允许不得转载。