我在这里有一个代表摇滚,纸,剪刀游戏的类。
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 场比赛后输出了谁赢得了最多的比赛。到目前为止,我已经完成了获胜的部分,但我无法弄清楚如何计算每场比赛的胜利并做到“三场比赛中最好的”。我该如何实施?