Do-While循环似乎不起作用

时间:2018-04-02 22:54:53

标签: java do-while

因此无论playerWins是返回true还是false,do while循环都会继续。我试图让它成为如果玩家获胜,游戏结束。我无法弄清楚我的生活有什么不对。我非常感谢任何帮助/提示。谢谢!

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);

1 个答案:

答案 0 :(得分:2)

当玩家获胜时,您必须退出外部for循环。将计数器设置为使for循环条件为false的值

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