简体   繁体   中英

Do-While loop doesn't seem to be working

So regardless of whether or not playerWins comes back as true or false, the do while loop continues. I'm trying to make it so that if the player wins, the game ends. I cannot figure out what's wrong for the life of me. I really appreciate any and all help/tips. Thank you!

int credibility = INITIAL_CREDIBILITY_LEVEL;


displayIntro();

userName = IR4.getString("\n" + "What are you called in your city?");

  for(counter = 1; counter <= NBR_OF_ROUNDS; counter ++)
  {
    do
    {
    if(counter == 1)
    {
      displayFirstRoundHeader(userName); 
    }
    else 
    {
      displayRoundHeader();
    }
    combination = IR4.getRandomNumber(STARTING_LOW, STARTING_HIGH);
    System.err.println(combination);
    playerWins = playRound(combination);

    credibility -= CREDIBILITY_LOST;

    if(playerWins)
      System.out.println("Yay you win");
    else if(credibility == 0)
    {
    displayRoundLossText(userName, credibility, combination);
    displayLossText(userName);
    }
    else
      displayRoundLossText(userName, credibility, combination);
    }
    while(!playerWins);

You have to exit out of the outer for loop when the player wins. Set the counter to a value that makes your for loop condition false

if(playerWins)
{
    System.out.println("Yay you win");
    counter = NBR_OF_ROUNDS + 1; //to break out of the outer most for loop
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM