簡體   English   中英

Do-While循環似乎無效

[英]Do-While loop doesn't seem to be working

因此,無論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);

玩家獲勝時,您必須退出外部for循環。 將計數器設置為使您的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