【发布时间】:2016-01-24 18:38:41
【问题描述】:
这是我的嵌套 for 循环。
int c = 1;
for (int d = 0; d < 10; d++) {
if (c == 6) {
System.out.println("Hello World 1");
} else if (c == 7) {
System.out.println("Hello World 2");
}
for (int e = 0; e < 5; e++) {
System.out.println("This is the nested for loop :" +c);
c++;
}
}
这是我的控制台正在打印的内容。
This is the nested for loop :1
This is the nested for loop :2
This is the nested for loop :3
This is the nested for loop :4
This is the nested for loop :5
Hello World 1
This is the nested for loop :6
This is the nested for loop :7
This is the nested for loop :8
它会继续打印,直到达到 50 并且不显示 Hello World 2。
【问题讨论】:
-
因为
else if执行时c永远不会是7。 -
那么,您是否在调试器中单步执行了每一行代码,并举例说明了变量值?这是要做的第一件事。
-
@OldProgrammer 现有的
println()语句足以用于此处的调试目的。
标签: java loops nested-loops