简体   繁体   English

java中循环if和else语句

[英]Looping if and else statements in java

I'm working on this program that asks for model numbers of cars infinitely until the person inputs 0 to break the loop.我正在研究这个程序,该程序无限地询问汽车的型号,直到该人输入 0 以打破循环。 When i run it and input a number it just infinitely loops either your car is defective or it is not defective until it crashes.当我运行它并输入一个数字时,它只是无限循环,要么你的车有缺陷,要么在崩溃之前没有缺陷。 I'm pretty stuck right now any help would be greatly appreciated.我现在很困,任何帮助将不胜感激。

Scanner input = new Scanner(System.in);

System.out.print("Enter a model number or 0 to quit: ");
modelNum = input.nextInt();

while (modelNum != 0) {

    if (modelNum >= 189 && modelNum <= 195) {
        System.out.println("Your car is defective it must be repaired");
    } else if (modelNum == 189 || modelNum == 221) {
        System.out.println("Your car is defective it must be repaired");
    } else if (modelNum == 780) {
        System.out.println("Your car is defective it must be repaired");
    } else if (modelNum == 119 || modelNum == 179) {
        System.out.println("Your car is defective it must be repaired");

    } else {
        System.out.println("Your car is not defective");
    }
    if (modelNum == 0) {
        System.out.println("end");
        break;
    }
}

It's because you never ask the user for another input.这是因为您从不要求用户提供其他输入。 You should do so before the end of the loop.您应该在循环结束之前这样做。

Include the this part into your loop:将此部分包含在您的循环中:

Scanner input = new Scanner(System.in);    

   System.out.print("Enter a model number or 0 to quit: ");
   modelNum = input.nextInt(); 

You have to ask for a new value to be evaluated:您必须要求评估一个新值:

while (modelNum != 0) {
    // if conditions
    modelNum = input.nextInt();
}

Also note that:另请注意:

if (modelNum == 0) {
    System.out.println("end");
    break;
}

won't be necessary because if the last value is 0 the condition in the while loop will be false and won't loop again.没有必要,因为如果最后一个值是0 ,while 循环中的条件将为假并且不会再次循环。

Last thing: why you have all those if-else-if when they all do the same thing (print "Your car is defective it must be repaired").最后一件事:当它们都做同样的事情时,为什么你拥有所有这些 if-else-if(打印“你的车有缺陷,必须修理”)。 This will be enough:这就足够了:

while (modelNum != 0) {
    if ((modelNum >= 189 && modelNum <= 195) || modelNum == 221 || modelNum == 780 || modelNum == 119 || modelNum == 179) {
        System.out.println("Your car is defective it must be repaired");
    } else {
        System.out.println("Your car is not defective");
    }
    modelNum = input.nextInt();
}

如果输入 0,循环将中断,因此最后一个 if 语句将永远不会运行。

This loop just tells you if the car is defective or not depending on the model number, but you never tell the program to exit the loop if the car is defective.这个循环只是根据型号告诉你汽车是否有缺陷,但如果汽车有缺陷,你永远不会告诉程序退出循环。 To do so you have to put break statements into each if statement of the loop.为此,您必须将 break 语句放入循环的每个 if 语句中。

Moreover this statement is useless:而且这个说法是没用的:

if(modelNum == 0) { System.out.println("end"); break;

since if u enter 0 the loop won't start.因为如果你输入 0 循环不会开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM