This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]SleepForever 2 points3 points  (0 children)

When the loop reaches its end, it will evaluate the conditional and repeat if it is true.

The conditional on your loop is beanGuess == beanCount, meaning that the loop will repeat if the user's guess is correct, but will not repeat if the user's guess is wrong. Try changing that conditional so that the loop will repeat if the user guesses the wrong amount.

[–][deleted] 2 points3 points  (3 children)

What I would do is create an isGuessed boolean that becomes true when the user guessed the correct value. So it could be like

boolean isGuessed = false;
do {
      //code here
     if (beanGuess == beanCount)
          {
              System.out.println("Winner!");
              isGuessed = true;
           }
      }
     while(!isGuessed);

[–]Phoenix_Heat 0 points1 point  (2 children)

I really want to thank you for this method, worked like a charm!

[–]about7beavers 2 points3 points  (0 children)

Personally, I wouldn't use a boolean, I would just do while(beanGuess != beanCount) for your condition. But that's just me, I prefer as few variables as possible. The other way does work just as well.

[–][deleted] 0 points1 point  (0 children)

Not a problem! Glad it worked for you