0

Okay so I am a total noob when it comes to Java so apologies if this is a stupid question. I have a while loop set up like so:

do {
  //quiz questions, lose a life when you get one wrong
} while (livesRemain > 0) 

However, when you lose all of your lives the loop does not end and continues through the loop even when all lives are lost. Would I use something else other than while or do while that will end when a certain condition is reached?

The questions are like this:

 System.out.print(question1) 
    if (guess == 1) {
    System.out.print("Correct")
    score++;
    } else {
    livesRemain--;
}

When you reach the end of the quiz, it prints your score which ends up going into the negatives if you go below 0.

8
  • Well ... You forgot the most important part in the body of your loop: The statement that changes the variable livesRemain. Maybe it isn't there? Commented Sep 24, 2015 at 16:46
  • 1
    If you want us to spot a bug in your code, post the code. Commented Sep 24, 2015 at 16:46
  • This snippet looks correct. Can you share the actual code of losing a life? Seems as though something went wrong there. Commented Sep 24, 2015 at 16:46
  • the while-loop looks fine. Have you made sure, that livesRemain is actually decremented? Maybe there exist two variables with the same name. Hard to tell though without code Commented Sep 24, 2015 at 16:47
  • I am 100% sure there is nothing wrong with your loop. Neither do you need to use other types of loop. The only reason in the universe why the loop is not ending is because livesRemain is still more than 0. Commented Sep 24, 2015 at 16:51

2 Answers 2

5

You should try something like:

do {
      if(wrong) 
        livesRemain--;
} while (livesRemain > 0) 
Sign up to request clarification or add additional context in comments.

Comments

1

The reason why the loop is not ending:

enter image description here

  • When you are inside the do-while loop, it will remain in the loop as long as the condition meets (i.e. liveRemaining > 0)
  • The variable livesRemaining is decremented outside the loop.
  • Hence, livesRemaining is always > 0 since you are not updating it anywhere in the loop.
  • Resulting loop not to end.

Would I use something else other than while or do while that will end when a certain condition is reached?

The issue is not with choosing a suitable loop. The problem is caused by terminating condition not updated. To solve this problem, bring your codes into the loop so that the terminating condition can be met.

do{
    //Ask question..
    if(guess == 1)
        System.out.println("Correct");
    else
        livesRemain--;     //Update livesRemain
}while(livesRemain > 0);   //Keeping asking questions till lives == 0   

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.