-8

我在这里有一个代表摇滚,纸,剪刀游戏的类。

public class RockPaperScissors{
    private String player1;
    private int player2;
    private String winner;

    public RockPaperScissors(){
        player1 = new String();
        player2 = (int)(Math.random()*3)+1;
        winner = new String();
    }

    public void setPlayer1(String player1){
        this.player1 = player1;
    }

    public void compute(){
        if(player1.equalsIgnoreCase("Rock")){
            if(player2 == 1){
                winner = "draw";
            }
            else if(player2 == 2){
                winner = "Player 2";
            }
            else{
                winner = "Player 1";
            }
        }
        else if(player1.equalsIgnoreCase("Paper")){
            if(player2 == 1){
                winner = "Player 1";
            }
            else if(player2 == 2){
                winner = "Draw";
            }
            else{
                winner = "Player 2";
            }
        }
        else if(player1.equalsIgnoreCase("Scissors")){
            if(player2 == 1){
                winner = "Player 2";
            }
            else if(player2 == 2){
                winner = "Player 1";
            }
            else{
                winner = "Draw";
            }
        }
        else{
            winner = "Sorry, you have enered an invalid option.";
        }
    }

    public String getWinner(){
        return winner;
    }

    public int getPlayer2(){
            return player2;
    }
}

我需要修改代码以跟踪用户赢得了多少场比赛以及计算机赢得了多少场比赛,并在玩了 3 场比赛后输出了谁赢得了最多的比赛。到目前为止,我已经完成了获胜的部分,但我无法弄清楚如何计算每场比赛的胜利并做到“三场比赛中最好的”。我该如何实施?

4

1 回答 1

0

您的解决方案有几个选项。

  • 跟踪两名玩家的胜利情况,并在每场比赛后检查他们赢得了多少。
  • 制作List你的游戏。
  • 做一个List你的赢家。

你可能想得太难了。为什么不创建一个int totalGames = 0int player1Wins = 0int player2Wins = 0在每场比赛后增加它们?

于 2013-11-14T12:56:05.023 回答