如何在 Java 中结束这个循环(更新任务。)

How to end this loop in Java (Updated Quest.)

我是初学者,第一次学习java。在用户输入 0 之前,我无法弄清楚如何让程序自行重复。 这是问题所在: 该应用程序应允许用户根据需要输入尽可能多的型号。使用 0 作为最终用户输入的标记。

输入汽车型号或0退出:195 你的车有缺陷。它必须被修复。 输入汽车的型号或 0 退出:119 你的车有缺陷。它必须被修复。 输入汽车的型号或 0 退出:0

 public class CarRecall
{
// Main method
public static void main(String[] args)
{
    int model; //model number
    Scanner input = new Scanner(System.in);

    do
    {
    System.out.print("Enter the car's model number or 0 to quit: ");
    model=input.nextInt();
    input.close();
    }   
    while (model>0);
    {

        if (model==119)
            {
                System.out.print("Your car is defective. It must be repaired.");
            }
       else if (model==179)
            {
                System.out.print("Your car is defective. It must be repaired.");
            }   
       else if (model>=189 && model<=195)
            {
                System.out.print("Your car is defective. It must be repaired.");
            }   
       else if (model==221)
            {
                System.out.print("Your car is defective. It must be repaired.");
            }
       else if (model==780)
            {
                System.out.print("Your car is defective. It must be repaired.");
            }     
       else 
             System.out.print("Your car is not defective.");
       model=0; 
    }

}

}

如果您想使用 while 循环,在这种情况下看起来没有必要,请将 Model 更改为 0 或添加 break

while(Model>0){//add the brace
   //...current if else code
   Model = 0;//or simply break;
}

--编辑--(使用 do while)

int Model; //model number
Scanner input = new Scanner(System.in);

do{
  System.out.print("Enter the car's model number or 0 to quit: ");
  Model=input.nextInt();

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

首先,似乎没有任何理由应该使用循环。 model 是一个数字,您可以对其进行检查,但这不会改变变量 model 的值。所以我建议删除整个循环。

如果你想用循环做一些事情,比如在某个时候调整变量model(但还没有实现),你可以使用break关键字来转义您目前居住的循环:

while(model > 0){
    if (some condition to finish looping){
        break;
    }
}

将代码更改为如下所示

        while (true) {
            System.out.print("Enter the car's model number or 0 to quit: ");
            model=input.nextInt();
            if (model == 0) {
                break; // This will break you out of the loop
            } else if (model == 119) {
                System.out.print("Your car is defective. It must be repaired.");
            } else if (model == 179) {
                System.out.print("Your car is defective. It must be repaired.");
            } else if (model >= 189 && model <= 195) {
                System.out.print("Your car is defective. It must be repaired.");
            } else if (model == 221) {
                System.out.print("Your car is defective. It must be repaired.");
            } else if (model == 780) {
                System.out.print("Your car is defective. It must be repaired.");
            } else
                System.out.print("Your car is not defective.");

        }
        // Move the scanner close to outside the loop to avoid `IllegalStateException`.
        input.close();

这将提示用户在每次迭代中输入一个新数字。当用户输入0时,会进入对应的if条件和break循环。希望这会有所帮助。

为了使循环 运行 永远存在,我将 while(model > 0) 更改为 while(true)。这将循环到无穷大,直到您输入“0”。

注意:我没有编译这段代码。只是摘录了您提出的问题。另外,我冒昧地将变量名从 Model 更改为 model。最好让变量名以小写字母开头。