My code below is from 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;
}
}
}
}
Output:
0 tom 0 jerry 1 tom 2 tom 2 jerry
I am learning java and I came across this. I don't understand why 1 tom
prints when the break is at 1
. If 1 tom
prints why then doesn't 1 jerry
?
对于x
每个值,您正在打印表sa
的全部内容,但x == 1除外,其中仅打印sa的第一个值:打印此值后,您要检查x == 1以及然后离开内部循环并继续x的下一个值。
First check "x == 1" then print.
String[] sa = { "tom ", "jerry " };
for (int x = 0; x < 3; x++) {
for (String s : sa) {
if (x == 1) {
break;
}
System.out.print(x + " " + s);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.