Java 为什么当我输入小于201时,它也会运行我不输入的最后一个if语句

Java 为什么当我输入小于201时,它也会运行我不输入的最后一个if语句,java,Java,我不明白为什么当我输入少于200时,这个代码不能正常工作,那么最后一个if语句也被执行了,谁能告诉我这个问题 此代码的主要问题是工作不正常 public class main { public static void main(String args[]){ Scanner input=new Scanner(System.in); double unit ; double extra; double total_unit; Sys

我不明白为什么当我输入少于200时,这个代码不能正常工作,那么最后一个if语句也被执行了,谁能告诉我这个问题

此代码的主要问题是工作不正常

    public class main {
    public static void main(String args[]){
    Scanner input=new Scanner(System.in);

    double unit   ;
    double extra;
    double total_unit;

    System.out.println("enter total unit");

    unit=input.nextInt();

    if(unit >=1 && unit <=200){

    unit=unit *8;

    System.out.println(" bill of 200 units is "+ unit);

    }

    if(unit  >=201 && unit <=300){

     extra = unit - 200;

     extra= extra * 10;

     total_unit = 200 * 8 + extra;

     System.out.println("Total bill is: " + total_unit); 

    }

    if(unit >=301 && unit<=400){

     extra=unit-300;

    extra=extra *15;

    total_unit=200*8 +100*10+ extra;

    System.out.println("total bill of more than 300 units is "+total_unit);

    }  

    if(unit >=401 && unit<=500){

     extra=unit-400;

    extra=extra*20;

     total_unit=200*8+ 100*10 + 100*15 + extra ;

    System.out.println("total bill between 401 to 500 units" + total_unit);

        }

     if(unit>501){  

     extra=unit-500;

    System.out.println("unit consumed  " + extra + "  that above");

    extra=extra *25;

    System.out.println("------------unit above 500 bill-------- \n"   +extra);
     total_unit=200*8 + 100*10 +100*15 +100*20 + extra;

    System.out.println("---------total bill----------\n  " +     total_unit);

    }

    }

    }

我已经在代码中写下了答案,为什么它要执行第二个条件,并读取代码中的注释。如果你有什么意见,那就发表评论吧

 //here your reading the unit value as ex: 190
 unit=input.nextInt(); 

 if (unit >= 1 && unit <= 200) {
        //here your changin the  unit value to 190 * 8 so now unit value is 1520
        unit = unit * 8;
        System.out.println(" bill of 200 units is " + unit);
  }
 //now here your given value is 190 but unit value is changed to 1520    here condition true, that is the reason second condition also excecuting.
 if (unit > 501) {
        extra = unit - 500;
        System.out.println("unit consumed  " + extra + "  that above");
        extra = extra * 25;
        System.out.println("------------unit above 500 bill-------- \n"
                + extra);
        total_unit = 200 * 8 + 100 * 10 + 100 * 15 + 100 * 20 + extra;
        System.out
                .println("---------total bill----------\n  " + total_unit);
    }

产生此问题的原因是,当您输入的数字小于201时,您的第一条if语句将按如下方式执行:

if(unit >=1 && unit <=200){
    unit=unit*8; //Problem is here. Use another variable such as unitTemp = unit*8
    System.out.println(" bill of 200 units is " + unit); //Use unitTemp here too
}
在此,任何小于200的数字将乘以8并分配给相同的单位变量,请注意,更新后的单位变量将在以下if条件中进一步检查,而不是用户输入的以前值


更新:我还建议在第一个if语句之后的每一个语句中使用else if,这样它只执行一次,因此您的问题就会消失,但这只是针对您的特定问题的一个技巧。如果您只使用这个else if解决方案,以便更好地获得有意义的代码,我建议同时使用这两种策略。这里不需要它,因为我的上述解决方案会有效地工作,但在不久的将来,您最终会这样做。

您还谈论了什么?您提供的代码中没有其他语句。请发布预期输出和实际输出。请提供正确缩进的代码。您将单位乘以8;进一步,如果语句正在测试更高的值。对乘法值使用不同的变量。丑陋、糟糕、格式错误的代码,其名称不符合Java编码标准。仔细阅读干燥原理。