中断嵌套的 for 循环
可能的重复:
如何中断主/外循环在双重/嵌套循环中?
我有以下情况:
for(int i = 0; i < schiffe.length-1; i++){
if(schiffe[i].schaden){
schlepper.fliege(schiffe[i].x,
schiffe[i].y,
schiffe[i].z);
schlepper.wirdAbgeschleppt = schiffe[i];
for(int k = 0; k < stationen.length-1; k++){
if(stationen[k].reparatur == null){
schlepper.fliege(stationen[k].x,
stationen[k].y,
stationen[k].z);
break;
}
}
}
}
我想
schlepper.fliege(stationen[k].x,
stationen[k].y,
stationen[k].z);
执行一次,然后跳出内部循环并继续 for(int i... 循环。所以我使用了中断但我不确定这是否正确。中断是否会导致所有循环中断或仅导致第二个循环中断?
Possible Duplicate:
How to Break from main/outer loop in a double/nested loop?
I have the following situation:
for(int i = 0; i < schiffe.length-1; i++){
if(schiffe[i].schaden){
schlepper.fliege(schiffe[i].x,
schiffe[i].y,
schiffe[i].z);
schlepper.wirdAbgeschleppt = schiffe[i];
for(int k = 0; k < stationen.length-1; k++){
if(stationen[k].reparatur == null){
schlepper.fliege(stationen[k].x,
stationen[k].y,
stationen[k].z);
break;
}
}
}
}
I want to
schlepper.fliege(stationen[k].x,
stationen[k].y,
stationen[k].z);
be performed once and then break out of the inner loop and continue with the for(int i... loop. So I used a break in my code. But I am not really sure if this is right. Does the break cause a break for all loops or just for the second loop?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
它仅破坏内部循环,因此可以执行您想要的操作。要打破多个级别,在 Java 中,您可以使用“带标签的中断”,如下所示:
但通常您不需要这个。
无论如何,您可以考虑为内部循环创建一个单独的方法;您可以轻松地为其命名(“find_available_station”或类似的名称),并且可能需要其他地方。
另外,请注意,您现在的循环将错过每个数组的最后一个元素。仔细考虑一下为什么要使用 <而不是 <=;正是这样,您可以使用 0 到 之间的每个值,但不包括指定的值。
It breaks only the inner loop, and therefore does what you want. To break more than one level, in Java, you can use a "labelled break" like so:
But usually you will not need this.
You may consider making a separate method for the inner loop anyway; it's something you can easily give a name to ("find_available_station" or something like that), and will probably need somewhere else.
Also, please be aware that your loops right now will miss the last element of each array. Think carefully about why you are using < instead of <=; it's exactly so that you use every value from 0 up to but not including the specified one.
break
将打破最里面的循环,在你的例子中是第二个循环。要使其中断外循环,您可以使用带标签的中断:
break
will break the innermost loop, in your case 2nd loop.To make it break the outer loop you can use a labeled break as:
break
仅跳出最内层循环。break
only breaks out of the innermost loop.中断只会导致最近的循环中断(第二个循环)
break will cause break for closest loop only (second loop)
只是第二个。当然,您可以使用“goto”。
Just the second one. You can use "goto" of course.
Break 语句终止最接近的封闭循环或 switch 语句,它出现在许多语言中,但您可以尝试一下并确保 100%。
Break statement terminates the closest enclosing loop or switch statement in which it appears in many languages but you can try it and be sure 100%.
将内部两个循环放入一个函数中,并使用 return 来打破这两个循环。
put the inner two loops in a function and use return to break out of both of them.
如果您需要更多控制,可以使用带标签的中断。更多信息请参见此处。正如其他人所说,break 将关闭最近的循环。
If you need more control, you can use a labeled break. More info here. As others have said, break will close the closest loop.
使用称为“found”的布尔值并像这样使用它怎么样:
What about using a boolean you call 'found' and use it like this:
我认为你还可以做一件事..
for(int k = 0; k < stationen.length-1; k++){
if(stationen[k].reparatur == null){
schlepper.fliege(stationen[k].x,
车站[k].y,
stationen[k].z);
I Think You Can Do One More Thing..
for(int k = 0; k < stationen.length-1; k++){
if(stationen[k].reparatur == null){
schlepper.fliege(stationen[k].x,
stationen[k].y,
stationen[k].z);