如何在Java中结束此循环(更新的Quest.)
我是一个初学者程序员,第一次学习java。在用户输入0之前,我不知道如何使程序自身重复。 问题是: 应用程序应允许用户根据需要输入尽可能多的型号。使用0作为最终用户输入的哨兵 输入汽车型号或0退出:195 你的车有毛病。它必须修理。 输入汽车型号或0退出:119 你的车有毛病。它必须修理。 输入汽车型号或0退出:0如何在Java中结束此循环(更新的Quest.),java,while-loop,do-while,Java,While Loop,Do While,我是一个初学者程序员,第一次学习java。在用户输入0之前,我不知道如何使程序自身重复。 问题是: 应用程序应允许用户根据需要输入尽可能多的型号。使用0作为最终用户输入的哨兵 输入汽车型号或0退出:195 你的车有毛病。它必须修理。 输入汽车型号或0退出:119 你的车有毛病。它必须修理。 输入汽车型号或0退出:0 如果您想使用while循环,在这种情况下看起来不需要将Model更改为0或添加中断 -编辑-使用do while int Model; //model number Scanner
如果您想使用while循环,在这种情况下看起来不需要将Model更改为0或添加中断 -编辑-使用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();
首先,似乎没有任何理由应该使用循环。模型是一个数字,您可以对其进行检查,但这不会改变变量模型的值。所以我建议删除整个循环 如果要对循环执行某些操作,例如在某个点调整变量模型,但尚未实现该操作,则可以使用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条件并中断循环。希望这有帮助
为了使循环永远运行,我将whilemodel>0更改为whiletrue。这将循环到无穷大,直到输入“0”
注意:我还没有编译这段代码。刚才从你提出的问题中摘录了一段。此外,我还随意地将变量名从一个模型更改为另一个模型。让变量名以小写字母开头是一种很好的做法。为什么需要在那里使用循环?如果只需与型号进行一次比较,则可以完全取消while循环。使用循环有什么特别的原因吗?你有一些混乱的大括号,比如导入java.util.Scanner后的大括号应该被删除,while循环需要一个起始大括号,修复这些问题,考虑如何用更有效的构造替换if-else。是@Rakesh,用户可以根据需要多次询问,直到键入zeroGoing(零)为止。按照您的编码方式,如果模型值小于或等于0,则不会输入循环,因此,如果model==0,则无法执行循环。如果您一直想要获取模型值并无限期地处理它,直到输入0为止,您必须在循环构造中移动model=input.nextInt。好的,它起作用了,但我需要它在一次操作中显示多次。因此,在输入汽车型号或0退出:,并显示消息后,程序将自动重复该操作,直到您键入0,然后添加System.out.Print输入汽车型号或0退出:;Model=input.nextInt;就在循环内部,然后可能希望在loopI测试此代码时将其更改为do,并且它可以正常工作,因此语法和逻辑良好
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();