【问题标题】:Simple Looping code won't work简单的循环代码不起作用
【发布时间】: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


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:
    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】:

      所以counter 最初是 9。 然后进入外层while循环。

      在里面,你递减counter 并检查它是否为4。因为它不是,并且EarthIsSpinning 设置为false,内部的while 循环没有被执行,你回到while 循环的开头。

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

      就像 Codebender 评论的那样,您可能需要 if 语句而不是 while。

      【讨论】:

        【解决方案4】:
        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");
              }
            }
          }
        }
        

        【讨论】:

          【解决方案5】:

          你为什么不这样做:

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

          【讨论】:

            【解决方案6】:

            有两种方法可以让你正确地做到这一点。将第二个 while 更改为 if 或保留第二个 while 但将标志 earthIsSpinning 更改为 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;
                    }
                }
            }
            

            【讨论】:

              【解决方案7】:

              你的代码中有逻辑错误

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

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

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

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2015-07-04
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多