Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
[Java]嵌套if-else语句+;多个System.out.print命令_Java_If Statement_Nested_Println - Fatal编程技术网

[Java]嵌套if-else语句+;多个System.out.print命令

[Java]嵌套if-else语句+;多个System.out.print命令,java,if-statement,nested,println,Java,If Statement,Nested,Println,我需要一些帮助来弄清楚为什么这不起作用。是否可能只是嵌套的if-else语句的规则,或者if-else语句通常只允许一个System.out.print语句? 我需要它输出 “可能有安全带。 可能有防抱死制动器。” 等它到了2000年 守则: public class CarFeatures { public static void main (String [] args) { int carYear = 0; carYear = 1990; if

我需要一些帮助来弄清楚为什么这不起作用。是否可能只是嵌套的if-else语句的规则,或者if-else语句通常只允许一个System.out.print语句? 我需要它输出 “可能有安全带。 可能有防抱死制动器。”

等它到了2000年

守则:

public class CarFeatures {
   public static void main (String [] args) {
      int carYear = 0;

      carYear = 1990;

      if (carYear <= 1969) {
         System.out.println("Probably has few safety features.");

      }else{
         if (carYear >= 1970) {
            System.out.println("Probably has seat belts.");

         }else{
            if (carYear >= 1990) {
               System.out.println("Probably has seat belts.");
               System.out.println("Probably has anti-lock brakes.");

         }else{
             if (carYear >= 2000) {
               System.out.println("Probably has seat belts.");
               System.out.println("Probably has anti-lock brakes.");
               System.out.println("Probably has air bags.");

             }
            }
         }
      }
      return;
   }
}
公共类功能{
公共静态void main(字符串[]args){
int carYear=0;
年份=1990年;
如果(卡年=1970){
System.out.println(“可能有安全带”);
}否则{
如果(年份>=1990){
System.out.println(“可能有安全带”);
System.out.println(“可能有防抱死制动器”);
}否则{
如果(年份>=2000){
System.out.println(“可能有安全带”);
System.out.println(“可能有防抱死制动器”);
System.out.println(“可能有安全气囊”);
}
}
}
}
返回;
}
}
因为

 if (carYear >= 1970) {
        System.out.println("Probably has seat belts.");

     }else{

     }

1990年>1970年,所以只需打印LN“可能有安全带。”

通过使用
if{…}else{…}
如果if`为真,则不会输入
else

所以把它们去掉

 if {
    ...
 }else{
     if (carYear >= 1970) {
        System.out.println("Probably has seat belts.");

     }
     if (carYear >= 1990) {
           System.out.println("Probably has seat belts.");
           System.out.println("Probably has anti-lock brakes.");

     }
      if (carYear >= 2000) {
           System.out.println("Probably has seat belts.");
           System.out.println("Probably has anti-lock brakes.");
           System.out.println("Probably has air bags.");

      }
 }

出现此问题的原因是
1990
大于或等于
1990
,但也大于或等于
1970
。因为
1970
语句首先出现在代码中,所以它永远不会到达
1990
语句

要解决此问题,代码中可能会出现各种优化:

  • 您可以在一行上声明并初始化
    carYear
    1990
  • 在嵌套级别使用if语句而不是else语句,以避免重复打印语句
  • 请尝试下面的代码

    公共类功能{
    公共静态void main(字符串[]args){
    int carYear=1990年;
    如果(卡年=1970){
    System.out.println(“可能有安全带”);
    } 
    如果(年份>=1990){
    System.out.println(“可能有防抱死制动器”);
    }
    如果(年份>=2000){
    System.out.println(“可能有安全气囊”);
    }
    }
    }
    }
    
    public class CarFeatures {
      public static void main (String [] args) {
        int carYear = 1990;
    
        if (carYear <= 1969) {
          System.out.println("Probably has few safety features.");
        } else {
          if (carYear >= 1970) {
            System.out.println("Probably has seat belts.");
          } 
          if (carYear >= 1990) {
            System.out.println("Probably has anti-lock brakes.");
          }
          if (carYear >= 2000) {
            System.out.println("Probably has air bags.");
          }
        }
      }
    }