循环很好。
while (!(list.contains("NORTH SOUTH") || list.contains("SOUTH NORTH") || list.contains("WEST EAST") || list.contains("EAST WEST"))) {
for (int i = 0; i < list.size(); i++) {
for (int k = i + 1; k < list.size(); k++) {
if (list.get(i).contains("NORTH") && list.get(k).contains("SOUTH") ||
list.get(i).contains("SOUTH") && list.get(k).contains("NORTH") ||
list.get(i).contains("WEST") && list.get(k).contains("EAST") ||
list.get(i).contains("EAST") && list.get(k).contains("WEST")) {
list.remove(i);
list.remove(k - 1);
}
}
}
我的问题是:
如何退出循环并保存结果列表?
答案 0 :(得分:3)
在if条件中使用break语句并将一个布尔值设置为一个变量。在for循环结束后检查该布尔变量的状态。如果其为true,则使用break语句中断while循环
while (!(list.contains("NORTH SOUTH") || list.contains("SOUTH NORTH") || list.contains("WEST EAST") || list.contains("EAST WEST"))) {
boolean conditionChecker=false;
for (int i = 0; i < list.size(); i++) {
for (int k = i + 1; k < list.size(); k++) {
if (list.get(i).contains("NORTH") && list.get(k).contains("SOUTH") ||
list.get(i).contains("SOUTH") && list.get(k).contains("NORTH") ||
list.get(i).contains("WEST") && list.get(k).contains("EAST") ||
list.get(i).contains("EAST") && list.get(k).contains("WEST")) {
list.remove(i);
list.remove(k - 1);
conditionChecker=true;
break;
}
}
if(conditionChecker==true){
break;
}
}
答案 1 :(得分:-2)
可能的副本: Break while