[英]Why doesn't this break
我下面的代码来自Oracle。
public class MyLoop {
public static void main(String[] args) {
String[] sa = {"tom ", "jerry "};
for(int x = 0; x < 3; x++) {
for(String s : sa) {
System.out.print(x + " " + s);
if(x == 1) break;
}
}
}
}
输出:
0 tom 0 jerry 1 tom 2 tom 2 jerry
我正在学习Java,并且遇到了这个问题。 我不明白为什么中断点为1
时会打印1 tom
。 如果打印了1 tom
,那么为什么没有1 jerry
?
对于x
每个值,您正在打印表sa
的全部内容,但x == 1除外,其中仅打印sa的第一个值:打印此值后,您要检查x == 1以及然后离开内部循环并继续x的下一个值。
首先检查“ x == 1”,然后打印。
String[] sa = { "tom ", "jerry " };
for (int x = 0; x < 3; x++) {
for (String s : sa) {
if (x == 1) {
break;
}
System.out.print(x + " " + s);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.