嵌套if-else语句始终是合法的,这意味着您可以在另一个if or else if语句中使用一个if or else if语句。
嵌套if ... else的语法如下-
if(Boolean_expression 1) { //当布尔表达式1为true时执行 if(Boolean_expression 2) { //当布尔表达式2为true时执行 } }
您可以嵌套else if ... else,就像我们嵌套if语句一样。
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