代码之家  ›  专栏  ›  技术社区  ›  llamagish

Do While循环似乎不起作用

  •  -1
  • llamagish  · 技术社区  · 7 年前

    因此,无论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 回复  |  直到 7 年前
        1
  •  2
  •   RAZ_Muh_Taz    7 年前

    当玩家获胜时,你必须退出外部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
    }