我正在研究这个程序,该程序无限地要求汽车的型号,直到人输入0打破循环为止。当我运行它并输入数字时,它会无限循环,直到您的汽车有故障或直到崩溃才是有缺陷的。我现在很困,任何帮助将不胜感激。

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;
    }
}

最佳答案

这是因为您从不要求用户提供其他输入。您应该在循环结束之前这样做。

10-08 12:59