解答
在最外层循环前加一个标记如outfor,然后用break outfor;可以跳出多重循环。
例如以下代码:
public class TestBreak { public static void main(String[] args) {
outfor: for (int i = 0; i < 10; i++){ for (int j = 0; j < 10; j++){ if (j == 5){ break outfor;
}
System.out.println("j = " + j);
}
}
}
}
运行结果如下所示:
j = 0
j = 1
j = 2
j = 3
j = 4
帖子还没人回复快来抢沙发