查看这种特定方法的输出让我感到困惑。我试图确切地了解它在做什么。
这是方法:
for (int i = 0; i < 4; i ++) {
for (int j = 0; j < 9; j++) {
if (j==5) {
break;
}
if ((j==0) || (i==0)) {
continue;
}
if (!((j*i % 3) != 0)) {
System.out.println(j*i);
}
}
}
我知道输出是什么,但我仍然不确定它是如何工作的,这就是我在这里问的原因。因此,此方法将遍历 j 直到达到 5,然后中断。
然后它增加 i,j 为 0。J != 5 所以它不会中断,j == 0 所以继续但是 J 变成 3,我是 1。这就是我迷路的地方。3是哪来的??
我用它来打印幕后发生的事情
public static void tester() {
for (int i = 0; i < 4; i ++) {
for (int j = 0; j < 9; j++) {
if (j==5) {
System.out.println("j == 5, breaking");
break;
}
if ((j==0) || (i==0)) {
System.out.println("J is " + j);
System.out.println("I is " + i);
System.out.println("j or i == 0, continuing");
continue;
}
if (!((j*i % 3) != 0)) {
System.out.println("J is " + j);
System.out.println("I is " + i);
System.out.println(j*i);
}
}
System.out.println();
}
}
输出如下:
J is 0
I is 0
j or i == 0, continuing
J is 1
I is 0
j or i == 0, continuing
J is 2
I is 0
j or i == 0, continuing
J is 3
I is 0
j or i == 0, continuing
J is 4
I is 0
j or i == 0, continuing
j == 5, breaking
J is 0
I is 1
j or i == 0, continuing
J is 3
I is 1
3
j == 5, breaking
J is 0
I is 2
j or i == 0, continuing
J is 3
I is 2
6
j == 5, breaking
J is 0
I is 3
j or i == 0, continuing
J is 1
I is 3
3
J is 2
I is 3
6
J is 3
I is 3
9
J is 4
I is 3
12
j == 5, breaking