2

This is the simple code that I was practicing on since I'm totally new to Java. This is under the Day8 component of the kilobot tutorial available online here: 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");
        }
    }
}

And I edited the supposed tutorial that I was supposed to do. so I was wondering why the console keeps looping "The earth is still spinning" and not only that at 4 where EarthIsSpinning = True, since EarthIsSpinning is only true when counter is at 4.

Pang
  • 9,564
  • 146
  • 81
  • 122
Wilson Six
  • 21
  • 1

7 Answers7

3

when counter = 4 it gets to this while loop

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

and it never exits that loop back into your original while loop the counter stays at 4 and EarthIsSpinning will stay true never exiting that while loop

JRowan
  • 6,824
  • 8
  • 40
  • 59
1

So counter is initially at 9. Then you enter the outer while loop.

Inside, you decrement counter and check if it's 4. Since it's not, and EarthIsSpinning is set to false the inner while loop isn't executed and you go back to the beginning of the while loop.

This repeats until counter becomes 4 at which point EarthIsSpinning is set to true, and the inner while loop runs forever since its value is never changed.

Like Codebender commented, you probably want an if statement instead of a while.

pushkin
  • 9,575
  • 15
  • 51
  • 95
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;
    }
    }
}
}
Chaya Sandamali
  • 657
  • 9
  • 22
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");
      }
    }
  }
}
FirebladeDan
  • 1,069
  • 6
  • 14
0

Why don't you do that:

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);
            }
        }
    }
}
zyftmx
  • 1
  • 1
0

There are two ways you can do make it correctly. Change the second while to if or keep the second while but change the flag earthIsSpinning to 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;
        }
    }
}
Kenny Tai Huynh
  • 1,464
  • 2
  • 11
  • 23
0

you have logic error in your code

this loop condition always true and wont stop forever :

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

its infinite loop , since when counter == 4 , the bool variable EarthIsSpinning = true; so when you enter the second while loop the condition wont break.

Oghli
  • 2,200
  • 1
  • 15
  • 37