gpt4 book ai didi

java - 简单循环代码不起作用

转载 作者:行者123 更新时间:2023-11-30 06:13:31 24 4
gpt4 key购买 nike

这是我练习的简单代码,因为我是 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 时才为真。

最佳答案

当 counter = 4 时它进入这个 while 循环

while (EarthIsSpinning){
System.out.println("The earth is still spinning");
}

它永远不会退出那个循环回到你原来的 while 循环计数器保持在 4 并且 EarthIsSpinning 将保持 true 永远不会退出 while 循环

关于java - 简单循环代码不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31737177/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com