Java中的“嵌套的if”语句是什么以及如何使用?

嵌套if-else语句始终是合法的,这意味着您可以在另一个if or else if语句中使用一个if or else if语句。

语法

if(Boolean_expression 1) {
   //当布尔表达式1为true时执行
   if(Boolean_expression 2) {
      //当布尔表达式2为true时执行
   }
}

示例

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