简体   繁体   English

我的循环再运行一遍,我看不出为什么

[英]My loop is running one extra time and I can't see why

I have a rock paper scissors game that is supposed to keep running until one player gets 4 consecutive wins. 我有一个剪刀石头布游戏,该游戏应该一直运行,直到一名玩家获得4连胜。 Everything seems to work correctly, but when one player gets the 4 consecutive wins, the game runs ONE extra time and I don't see why. 一切似乎都可以正常运行,但是当一名玩家获得了连续4场胜利时,游戏就会多运行一次,我不知道为什么。

//THE WHILE STATEMENT //当声明

while(!done){       
    done=play1Win.consecutiveWins==4||play2Win.consecutiveWins==4;
    player1Roll=random();
    player2Roll=random();

//THIS STUFF IS ALL INSIDE THE WHILE LOOP BUT I WILL ONLY POST A SECTION OF IT. //这个草稿都在循环中,但是我只会张贴它的一部分。 IT'S ALL THE SAME 全部都是一样

        if(player1Roll==SCISSORS&&player2Roll==PAPER){
        System.out.println("Scissors beats Paper! Player 1 wins");      
        lastWinner=PLAYER1;

        if (lastWinner==PLAYER1){
        play1Win.consecutiveWins++;
        play2Win.consecutiveWins=0;
        }
    }
    if(player1Roll==PAPER&&player2Roll==ROCK){
        System.out.println("Paper beats Rock! Player 1 wins");      
        lastWinner=PLAYER1;

        if (lastWinner==PLAYER1){
        play1Win.consecutiveWins++;
        play2Win.consecutiveWins=0;
        }

//AT THE END OF THE LOOP IS THIS //这是循环的尽头

      System.out.println("Player 1 wins- " +play1Win.consecutiveWins );  
      System.out.println("Player 2 wins- " +play2Win.consecutiveWins);

And the output is this: 输出是这样的:

    Paper beats Rock! Player 2 wins
    Player 1 wins- 0
    Player 2 wins- 1
    Paper beats Spock! Player 2 wins
    Player 1 wins- 0
    Player 2 wins- 2
    Rock beats Scissors! Player 2 wins
    Player 1 wins- 0
    Player 2 wins- 3
    Rock beats Scissors! Player 2 wins
    Player 1 wins- 0
    Player 2 wins- 4     // SHOULD STOP HERE
    Paper beats Rock! Player 1 wins    
    Player 1 wins- 1
    Player 2 wins- 0

If anyone can point me in the right direction, that would be great. 如果有人能指出我正确的方向,那就太好了。 Thanks! 谢谢!

您应该将此行放在while循环的最后一行

done=play1Win.consecutiveWins==4||play2Win.consecutiveWins==4;

You set your done condition at the beginning of the loop. 您可以在循环开始时设置完成条件。 This means that even though player2 has 4 consecutive wins, done doesn't get set to true until the next loop has already started. 这意味着即使player2连续获得了4次胜利,在下一个循环开始之前,done都不会设置为true。 move this line to the end of the while loop: 将此行移到while循环的末尾:

  done=play1Win.consecutiveWins==4||play2Win.consecutiveWins==4;

To close the loop immediately after 4 consecutive wins, exit criteria should be checked at the end of the loop not at the beginning of the NEXT loop. 要在连续四次获胜后立即关闭循环,应在循环结束时而不是在NEXT循环开始时检查退出标准。 If you check exit criteria at the beginning of the loop, make it part of your while like rewriting while (!done){...} 如果您在循环开始时检查退出条件,则将其作为您的一部分,就像重写while (!done){...}

to

while(play1Win.consecutiveWins==4||play2Win.consecutiveWins==4){ ... }

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

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