Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 简单循环代码赢得';行不通_Java - Fatal编程技术网

Java 简单循环代码赢得';行不通

Java 简单循环代码赢得';行不通,java,Java,这是我练习的简单代码,因为我对Java完全陌生。这是在线提供的kilobot教程第8天部分: 我编辑了我应该做的教程。所以我想知道为什么控制台一直循环“地球仍在旋转”,而不仅仅是在4时,其中EarthIsSpinning=True,因为EarthIsSpinning只有在计数器为4时才是真的。当计数器为4时,它会进入这个while循环 while (EarthIsSpinning){ System.out.println("The earth is still spinning"

这是我练习的简单代码,因为我对Java完全陌生。这是在线提供的kilobot教程第8天部分:


我编辑了我应该做的教程。所以我想知道为什么控制台一直循环“地球仍在旋转”,而不仅仅是在4时,其中EarthIsSpinning=True,因为EarthIsSpinning只有在计数器为4时才是真的。

当计数器为4时,它会进入这个while循环

while (EarthIsSpinning){
        System.out.println("The earth is still spinning");
        }
它永远不会退出那个循环回到你原来的while循环中
计数器保持在4,接地旋转将保持为真,当循环时不会退出,因此
计数器最初为9。
while (EarthIsSpinning){
        System.out.println("The earth is still spinning");
        }
然后进入外部while循环

在内部,将计数器减量
并检查它是否为4。因为它不是,并且
EarthIsSpining
设置为
false
内部while循环不会执行,因此返回while循环的开头

这将重复进行,直到计数器变为4,此时
EarthIsSpinning
设置为true,并且内部while循环将永远运行,因为其值从未更改

就像Codebender评论的那样,您可能需要一个if语句而不是一段时间。

为什么不这样做:

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;
      }

      if(EarthIsSpinning)
      {
         System.out.println("The earth is still spinning");
      }
    }
  }
}
public class Looping {
    public static void main(String args[]) {
        int counter = 9;

        while(counter >= 1){
            counter--;
            if (counter == 4){
               System.out.println("The earth is still spinning");
            }else{
               System.out.println(counter);
            }
        }
    }
}

有两种方法可以使它正确。将第二个while更改为if,或保留第二个while,但将标志EarthIsSpining更改为false

public static void main(String args[]) {
        //flag to check the Spin
        boolean earthIsSpinning = false;
        //init the counter
        int counter = 9;

        //loop until counter is 0
        while(counter >= 1){
            counter--;
            System.out.println(counter);

            //condition to change flag to true
            if (counter == 4){
                earthIsSpinning = true;
            } else{
                earthIsSpinning = false;
            }
            //print the message if flag is true
            if (earthIsSpinning){
                System.out.println("The earth is still spinning: " + counter);
            }
        }
    }

Or you can do

public static void main(String args[]) {
    //flag to check the Spin
    boolean earthIsSpinning = false;
    //init the counter
    int counter = 9;

    //loop until counter is 0
    while(counter >= 1){
        counter--;
        System.out.println(counter);

        //condition to change flag to true
        if (counter == 4){
            earthIsSpinning = true;
        } else{
            earthIsSpinning = false;
        }
        //print the message if flag is true
        while (earthIsSpinning){
            System.out.println("The earth is still spinning: " + counter);
            earthIsSpinning = false;
        }
    }
}

您的代码中有逻辑错误

此循环条件始终为真,不会永远停止:

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

它的无限循环,因为当
计数器==4
时,bool变量
地球自旋=true因此,当您进入第二个while循环时,条件不会中断。

还需要一个
while
吗?您需要
if(接地旋转)
。不
while
。请查看地球旋转while在主while循环内,因此内部while需要停止以移动外部while循环的下一次迭代
while (EarthIsSpinning){
           System.out.println("The earth is still spinning");
        }