Java 满足条件后如何跳出循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16269589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to break from a loop after the condition is met
提问by user2252310
Hi I have been trying for the past hour to break from this loop and continue since already met my condition once. My application pretty much reads a serie of lines and analyzes it and then prints the variable stated. An example of how the lines look like (the . are not included):
嗨,过去一个小时我一直在尝试打破这个循环并继续,因为已经满足我的条件一次。我的应用程序几乎读取一系列行并对其进行分析,然后打印所述变量。线条外观的示例(不包括 .):
- 10 c = 9+3
- 20 a = c+1
- 30 print c
- 40 goto 20
- 50 end
- 10 c = 9+3
- 20 a = c+1
- 30 打印 c
- 40 转 20
- 50 结束
It does everything right, when it gets to line 40 goes to line 20 as expected, but i want it to go to line 50 since already went to line 40 once. Here is my code for this part:
它做的一切都是正确的,当它到达第 40 行时按预期转到第 20 行,但我希望它转到第 50 行,因为它已经去过第 40 行一次。这是我的这部分代码:
while(booleanValue)
{
if(aString.substring(0, 4).equals("goto"))
{
int chosenLine = Integer.parseInt(b.substring(5));
if(inTheVector.contains(chosenLine))
{
analizeCommands(inTheVector.indexOf(chosenLine));
i++;
}
else
{
System.ou.println("Line Was Not Found");
i++;
}
}
else if(aString.substring(0, 3).equals("end"))
{
System.out.println("Application Ended");
booleanValue = false;
}
}
回答by MarsAtomic
Use the break
statement to break out of a loop completely once your condition has been met. Pol0nium's suggestion to use continue
would not be correct since that stops the current iteration of the loop only.
break
一旦满足您的条件,使用该语句即可完全跳出循环。Pol0nium 的使用建议是continue
不正确的,因为这只会停止循环的当前迭代。
while(foo)
{
if(baz)
{
// Do something
}
else
{
// exit condition met
break;
}
}
All this having been said, good form dictates that you want clean entry and exit points so that an observer (maybe yourself, revisiting the code at a later date) can easily follow its flow. Consider altering the boolean that controls the while loop itself.
综上所述,良好的形式表明您需要干净的入口和出口点,以便观察者(可能是您自己,稍后重新访问代码)可以轻松地遵循其流程。考虑更改控制 while 循环本身的布尔值。
while(foo)
{
if(baz)
{
// Do something
}
else
{
// Do something else
foo = false;
}
}
If, for some reason, you can't touch the boolean that controls the while loop, you need only compound the condition with a flag specifically to control your while:
如果由于某种原因,您无法触摸控制 while 循环的布尔值,则只需将条件与专门用于控制 while 的标志复合:
while(foo && bar)
{
if(baz)
{
// Do something
}
else
{
// Do something else
bar = false;
}
}
回答by Scott Heaberlin
You could keep the code using booleanValue
as-is and switch to a do-while loop.
您可以booleanValue
按原样保留代码并切换到 do-while 循环。
do {
// ... existing code
} while (booleanValue);
However, to answer your specific question - you can always use the java break
keyword. The continue
keyword is more for skipping the remainder of the loop block and entering another loop iteration.
但是,要回答您的特定问题 - 您始终可以使用 javabreak
关键字。该continue
关键字更多地用于跳过循环块的其余部分并进入另一个循环迭代。
if you put this before your check for anything else, you would exit the loop immediately.
如果你把它放在你检查其他任何东西之前,你会立即退出循环。
if(aString.substring(0, 3).equals("end")) {
break;
}
As an additional tip, you may want to use String.contains("end")or String.endsWith("end")instead of substring(). This way if a 1 or 3 digit (or more) number is used your code will still work.
作为附加提示,您可能需要使用String.contains("end")或String.endsWith("end")而不是 substring()。这样,如果使用 1 位或 3 位(或更多)数字,您的代码仍然有效。