简体   繁体   English

Do-While循环似乎无效

[英]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. 因此,无论playerWins是否返回true或false,do while循环都会继续。 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. 玩家获胜时,您必须退出外部for循环。 Set the counter to a value that makes your for loop condition false 将计数器设置为使您的for循环条件为假的值

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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