简单的循环代码不会起作用

时间:2015-07-31 02:30:54

标签: java

这是我练习的简单代码,因为我对Java完全陌生。这是在线提供的千字节教程的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");
        }
    }
}

我编辑了我应该做的假设教程。所以我想知道为什么控制台会循环播放"地球仍在旋转"而且不仅是在4,EarthIsSpinning = True,因为只有当计数器为4时,EarthIsSpinning才为真。

7 个答案:

答案 0 :(得分:3)

当counter = 4时,它会进入while循环

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

它永远不会退出循环回原来的循环 计数器保持在4,EarthIsSpinning将保持不变,从不退出循环

答案 1 :(得分:1)

所以counter最初为9。 然后输入外部while循环。

在内部,您递减counter并检查它是否为4.由于它不是,EarthIsSpinning设置为false内部while循环isn&# 39; t执行,然后你回到while循环的开头。

重复直到计数器变为4,此时EarthIsSpinning设置为true,内部while循环永远运行,因为它的值永远不会改变。

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

答案 2 :(得分:1)

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;
            System.out.println("The earth is still spinning");
        }
    else{
        EarthIsSpinning = false;
    }
    }
}
}

答案 3 :(得分:0)

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

答案 4 :(得分:0)

你为什么不这样做:

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

答案 5 :(得分:0)

有两种方法可以正确完成。将第二个更改为if或保持第二个while,但将flagIsSpinning标志更改为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;
        }
    }
}

答案 6 :(得分:0)

您的代码中存在逻辑错误

这种循环条件总是正确的,不会永远停止:

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

它的无限循环,因为当counter == 4时,bool变量EarthIsSpinning = true;所以当你输入第二个while循环时,条件不会中断。