Java 如何从另一个循环中停止一个循环?
因此,我在while循环中运行for循环。如果我的if()在for循环中找到了什么,它应该停止for和while循环。 顺便说一句,这是Java。 这是我的代码:Java 如何从另一个循环中停止一个循环?,java,loops,break,Java,Loops,Break,因此,我在while循环中运行for循环。如果我的if()在for循环中找到了什么,它应该停止for和while循环。 顺便说一句,这是Java。 这是我的代码: while(!(objecFound)){ for(int i = 0; i <= currObsTile.length; i++){ if(currObsTile[i].tile == endTile){ //If this is
while(!(objecFound)){
for(int i = 0; i <= currObsTile.length; i++){
if(currObsTile[i].tile == endTile){
//If this is true, it needs to stop the while loop
}
}
}
while(!(objecFound)){
对于(int i=0;i,您可以使用Java标签解决此问题:
OuterLoop:
while(!(objecFound)){
for(int i = 0; i <= currObsTile.length; i++){
if(currObsTile[i].tile == endTile){
//If this is true, it needs to stop the while loop
break OuterLoop;
}
}
}
OuterLoop:
while(!(objecFound)){
for(int i=0;我在for循环中添加了另一个条件来检查它是否应该停止,可能是像outer一样的objecFound
。或者break
out-itreturn;是一个很好的方法,但是我认为这会将您踢出整个方法。在您的情况下,objecFound=true