【发布时间】:2015-08-01 08:44:41
【问题描述】:
这是我练习的简单代码,因为我是 Java 新手。这是 Kilobot 教程的 Day8 组件,可在此处在线获取:http://www.kilobolt.com/day-8-looping
public class Looping {
public static void main(String args[]) {
boolean EarthIsSpinning = false;
int counter = 9;
while(counter >= 1){
counter--;
System.out.println(counter);
if (counter == 4){
EarthIsSpinning = true;
}
else{
EarthIsSpinning = false;
}
while (EarthIsSpinning){
System.out.println("The earth is still spinning");
}
}
}
我编辑了我应该做的假定教程。所以我想知道为什么控制台不断循环“地球仍在旋转”,而不仅仅是在 EarthIsSpinning = True 的 4 处循环,因为 EarthIsSpinning 仅在计数器为 4 时才为真。
【问题讨论】:
-
还需要一个
while吗? -
你需要
if(EarthIsSpinning)。不是while。 -
请查看 EarthIsSpinning while 位于主 while 循环内,因此内部 while 需要停止以移动外部 while 循环的下一次迭代
标签: java