计数器在执行过程中重置。

huangapple go评论62阅读模式
英文:

Counter resets during execution

问题

以下是翻译好的代码部分:

public class PaperRockGame {

    private static final int SCISSOR = 1;
    private static final int LIZARD = 2;
    private static final int PAPER = 3;
    private static final int ROCK = 4;
    private static final int SPOCK= 5;
    private static final int PLAYER1 = 1;
    private static final int PLAYER2 = 2;
    private String cpuChoice1;
    private String cpuChoice2;
    private int player1Score;
    private int player2Score;
    private int lastWinner;

    public void setCpuOne() {
        String choice;
        choice = numberToString(randomNumber());
        cpuChoice1 = choice;	
    }

    public void setCpuTwo() {
        String choice;
        choice = numberToString(randomNumber());
        cpuChoice2 = choice;
    }

    public String getCpuOneValue() {
        return cpuChoice1;
    }

    public String getCpuTwoValue() {
        return cpuChoice2;
    }

    public int getP1Score() {
        return player1Score;
    }

    public int getP2Score() {
        return player2Score;
    }

    public int randomNumber() {
        int number = (int)(Math.random() * 5 + 1 );
        return number;
    }

    public String numberToString(int value) {
        int number = value;
        if (number == 1) {
            return "SCISSOR";
        }
        else if(number == 2) {
            return "LIZARD";
        }
        else if(number == 3) {
            return "PAPER";
        }
        else if(number == 4) {
            return "ROCK";
        }
        else { 
            return "SPOCK";
        }
    }

    public void playGame(int cpuOne, int cpuTwo) {
        if(cpuOne == cpuTwo) {
            System.out.println("Tie");
        }
        else if ((cpuOne ==  ROCK && ( cpuTwo == SCISSOR || cpuTwo ==  LIZARD))
            ||(cpuOne ==  SCISSOR && ( cpuTwo == PAPER || cpuTwo == LIZARD))
            ||(cpuOne ==  PAPER && ( cpuTwo == ROCK || cpuTwo == SPOCK))
            ||(cpuOne ==  LIZARD && ( cpuTwo == PAPER || cpuTwo == SPOCK))
            ||(cpuOne ==  SPOCK && ( cpuTwo == SCISSOR || cpuTwo == ROCK))){

            System.out.println("Player 1 wins");
            if(lastWinner == PLAYER1) {
                player1Score++;
            }
            else {
                player1Score = 1;
            } 
            lastWinner = PLAYER1;
        }
        else {
            System.out.println("Player 2 wins");
            if(lastWinner == PLAYER2) {
                player2Score++;
            }
            else {
                player2Score = 1;
            }
            lastWinner = PLAYER2;
        }
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int x = 0;
        PaperRockGame play = new PaperRockGame ();

        do {
            play.setCpuOne();
            play.setCpuTwo();
            System.out.println(play.getCpuOneValue());
            System.out.print(play.getCpuTwoValue());
            System.out.println();
            play.playGame(play.randomNumber(), play.randomNumber());
            System.out.println(play.getP1Score() + " " + play.getP2Score());
            x++;
        } while(x < 100);
    }
}
英文:

I am developing a Paper Rock Spock...game. However the score counter keeps resetting throughout. Can someone shed some light on how I can fix this please? The playGame() method takes two random numbers and match them against known constants. Then it should increment the player1 or player2 score depending on the output of the logical statement.

public class PaperRockGame {
private static final int SCISSOR = 1;
private static final int LIZARD = 2;
private static final int PAPER = 3;
private static final int ROCK = 4;
private static final int SPOCK= 5;
private static final int PLAYER1 = 1;
private static final int PLAYER2 = 2;
private String cpuChoice1;
private String cpuChoice2;
private int player1Score;
private int player2Score;
private int lastWinner;
//private int player2Score;
public void setCpuOne() {
String choice;
choice = numberToString(randomNumber());
cpuChoice1 = choice;	
}
public void setCpuTwo() {
String choice;
choice = numberToString(randomNumber());
cpuChoice2 = choice;
}
public String getCpuOneValue() {
return cpuChoice1;
}
public String getCpuTwoValue() {
return cpuChoice2;
}
public int getP1Score() {
return player1Score;
}
public int getP2Score() {
return player2Score;
}
public int randomNumber() {
int number = (int)(Math.random() * 5 + 1 );
return number;
}
public String numberToString(int value) {
int number = value;
if (number == 1) {
return &quot;SCISSOR&quot;;
}
else if(number == 2) {
return &quot;LIZARD&quot;;
}
else if(number == 3) {
return &quot;PAPER&quot;;
}
else if(number == 4) {
return &quot;ROCK&quot;;
}
else { 
return &quot;SPOCK&quot;;
}
}
public void playGame(int cpuOne, int cpuTwo) {
if(cpuOne == cpuTwo) {
System.out.println(&quot;Tie&quot;);
}
else if ((cpuOne ==  ROCK &amp;&amp; ( cpuTwo == SCISSOR || cpuTwo ==  LIZARD))
||(cpuOne ==  SCISSOR &amp;&amp; ( cpuTwo == PAPER || cpuTwo == LIZARD))
||(cpuOne ==  PAPER &amp;&amp; ( cpuTwo == ROCK || cpuTwo == SPOCK))
||(cpuOne ==  LIZARD &amp;&amp; ( cpuTwo == PAPER || cpuTwo == SPOCK))
||(cpuOne ==  SPOCK &amp;&amp; ( cpuTwo == SCISSOR || cpuTwo == ROCK))){
System.out.println(&quot;Player 1 wins&quot;);
if(lastWinner == PLAYER1) {
player1Score++;
}
else {
player1Score = 1;
} 
lastWinner = PLAYER1;
}
else {
System.out.println(&quot;Player 2 wins&quot;);
if(lastWinner == PLAYER2) {
player2Score++;
}
else {
player2Score = 1;
}
lastWinner = PLAYER2;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int x = 0;
PaperRockGame play = new PaperRockGame ();
do {
play.setCpuOne();
play.setCpuTwo();
System.out.println(play.getCpuOneValue());
System.out.print(play.getCpuTwoValue());
System.out.println();
play.playGame(play.randomNumber(), play.randomNumber());
System.out.println(play.getP1Score() + &quot; &quot; + play.getP2Score());
x++;
} while(x &lt; 100);
}
}

答案1

得分: 0

如果当前赢家与上一位赢家不同,您将该玩家的得分重置为一。

默认情况下,Java中原始int的值为0。您可以简单地对其进行递增。

然后只需移除if(lastWinner...条件,并始终执行player1Score++player2Score++

英文:

If the current winner differs from the last winner, you are resetting the score for that player to one.

<strike>
I think instead you should initialize player1Score and player2Score to 0.

private int player1Score=0;
private int player2Score=0;

</strike>

The default value of a primitive int in Java is 0. You can simply increment it.

Then simply remove the if(lastWinner... conditional and always do either player1Score++ or player2Score++.

答案2

得分: 0

删除lastWinnerint类型变量。您不需要它来执行所需的记分逻辑。这意味着您还可以摆脱使用此变量的任何代码。

就这样说吧。如果玩家1获胜,只需增加玩家1的分数。没有人关心谁赢得了上一场比赛。所有比赛结束时的总分将最终讲述故事。同样,如果玩家2获胜,只需增加玩家2的分数。所以......

public void playGame(int cpuOne, int cpuTwo) {
    if(cpuOne == cpuTwo) {
        System.out.println("平局");
    }
    else if ((cpuOne ==  ROCK && ( cpuTwo == SCISSOR || cpuTwo ==  LIZARD))
        ||(cpuOne ==  SCISSOR && ( cpuTwo == PAPER || cpuTwo == LIZARD))
        ||(cpuOne ==  PAPER && ( cpuTwo == ROCK || cpuTwo == SPOCK))
        ||(cpuOne ==  LIZARD && ( cpuTwo == PAPER || cpuTwo == SPOCK))
        ||(cpuOne ==  SPOCK && ( cpuTwo == SCISSOR || cpuTwo == ROCK))){

        System.out.println("玩家1获胜");
        player1Score++;
    }
    else {
        System.out.println("玩家2获胜");
        player2Score++;
    }
}
英文:

Get rid of the lastWinner int type variable. You just don't need it to carry out the required logic for score keeping. This then means that you can also get rid of any code that uses this variable.

Let's put it this way. If Player 1 wins then simply increment the Player 1 score. No one cares who won the previous game. The overall score at the end of all games will finally tell the tale. On the same token, if Player 2 wins then simply increment the Player 2 score. So.....

public void playGame(int cpuOne, int cpuTwo) {
if(cpuOne == cpuTwo) {
System.out.println(&quot;Tie&quot;);
}
else if ((cpuOne ==  ROCK &amp;&amp; ( cpuTwo == SCISSOR || cpuTwo ==  LIZARD))
||(cpuOne ==  SCISSOR &amp;&amp; ( cpuTwo == PAPER || cpuTwo == LIZARD))
||(cpuOne ==  PAPER &amp;&amp; ( cpuTwo == ROCK || cpuTwo == SPOCK))
||(cpuOne ==  LIZARD &amp;&amp; ( cpuTwo == PAPER || cpuTwo == SPOCK))
||(cpuOne ==  SPOCK &amp;&amp; ( cpuTwo == SCISSOR || cpuTwo == ROCK))){
System.out.println(&quot;Player 1 wins&quot;);
player1Score++;
}
else {
System.out.println(&quot;Player 2 wins&quot;);
player2Score++;
}
}

huangapple
  • 本文由 发表于 2020年4月9日 10:47:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/61113168.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定