gpt4 book ai didi

java - 如何在双/嵌套循环中脱离主/外循环?

转载 作者:IT老高 更新时间:2023-10-28 20:47:25 24 4
gpt4 key购买 nike

如果我在循环中有循环,并且一旦满足 if 语句我想打破主循环,我应该怎么做?

这是我的代码:

for (int d = 0; d < amountOfNeighbors; d++) {
for (int c = 0; c < myArray.size(); c++) {
if (graph.isEdge(listOfNeighbors.get(d), c)) {
if (keyFromValue(c).equals(goalWord)) { // Once this is true I want to break main loop.
System.out.println("We got to GOAL! It is "+ keyFromValue(c));
break; // This breaks the second loop, not the main one.
}
}
}
}

最佳答案

使用带标签的中断:

mainloop:
for(){
for(){
if (some condition){
break mainloop;
}
}
}

另见

关于java - 如何在双/嵌套循环中脱离主/外循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13073300/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com