# Dice game between 2 players up to a score of 50

go评论60阅读模式

Dice game between 2 players up to a score of 50

# 问题

``````public static void main(String[] args) {
int playerA[] = new int[10];
int playerB[] = new int[10];
int playerAScore = 0;
int playerBScore = 0;
int round = 1;

for (int i = 0; i < playerA.length; i++) {
System.out.println("Roll the die for round " + round++);
playerA[i] = (int) ((Math.random() * 6) + 1);
playerB[i] = (int) ((Math.random() * 6) + 1);

System.out.println("player A has " + playerA[i] + " and player B has " + playerB[i]);

if (playerA[i] == playerB[i]) {
playerAScore = playerAScore + 3;
playerBScore = playerBScore + 3;
} else if (playerA[i] > playerB[i]) {
playerAScore = playerAScore + 5;
} else if (playerB[i] > playerB[i]) {
playerBScore = playerBScore + 5;
}
if (playerAScore >= 50 || playerBScore >= 50) {
break;
}
}

System.out.println("The game is over.");

if (playerAScore >= playerBScore)
System.out.println("The winner is player A");
else
System.out.println("The winner is player B");

System.out.println("How many rounds of game played?");
System.out.println("Rounds played: " + round);

System.out.println("Total Score per player:");
System.out.println("Player A score: " + playerAScore);
System.out.println("Player B score: " + playerBScore);
}
``````

I am creating a game where two players are rolling dice against each other.

Two people (player A, player B) play a dice game. they roll a dice each round and the higher number wins the game and the winner earn 5 points.If two people roll the same value of dice, they both earn 3 points. The game ends if one player reaches 50 points or higher. If both players reach 50 at the same round, that becomes deuce and one more round of game to go until higher value player wins the game (if the 11th round still tie, play next round. Here is my code so far, but I am not getting any of the scores to go to 50 to end the game.

``````public static void main(String[] args) {
int playerA[] = new int[10];
int playerB[] = new int[10];
int playerAScore = 0;
int playerBScore = 0;
int round = 1;
for (int i =0; i &lt; playerA.length; i++) {
System.out.println(&quot;Roll the die for round &quot; + round++);
playerA[i] = (int) ((Math.random() * 6) + 1);
playerB[i] = (int) ((Math.random() * 6) + 1);
System.out.println(&quot;player A has &quot; + playerA[i] + &quot; and player B has &quot; + playerB[i]);
if(playerA[i] == playerB[i]) {
playerAScore = playerAScore + 3;
playerBScore = playerBScore + 3;
}
else if (playerA[i] &gt; playerB[i]) {
playerAScore = playerAScore + 5;
}
else if (playerB[i] &gt; playerB[i]) {
playerBScore = playerBScore + 5;
}
if(playerAScore &gt;= 50 || playerBScore &gt;= 50) {
break;
}
}
System.out.println(&quot;The game is over.&quot;);
if(playerAScore &gt;= playerBScore)
System.out.println(&quot;The winner is player A&quot;);
else
System.out.println(&quot;The winner is player B&quot;);
System.out.println(&quot;How many rounds of game played?&quot;);
System.out.println(&quot;Rounds played: &quot; + round);
System.out.println(&quot;Total Score per player:&quot;);
System.out.println(&quot;Player A score: &quot; + playerAScore);
System.out.println(&quot;Player B score: &quot; + playerBScore);
}
``````

}

# 答案1

1. 在比分比较中，您有一个拼写错误。

`playerB[i] &gt; playerB[i]`

``````    playerBScore = playerBScore + 5;
}
``````

``````    playerBScore = playerBScore + 5;
}
``````
1. 第二个错误可能是您只玩了`playerA.length`次。在这种情况下是10，所以只有在玩家赢得所有比赛的情况下，他才能得到50分（这个概率非常非常低）。

2. 结尾显示的回合数是错误的。已经玩了10轮，但增加了一轮，所以显示的回合数是`11`

1. In the comparison between scores you have a typo.
``````playerB[i] &gt; playerB[i]
``````

which is always false, so one player doesn't get his points at all.

``````        else if (playerB[i] &gt; playerB[i]) {
playerBScore = playerBScore + 5;
}
``````

fragment. Which obviously should be:

``````        else if (playerB[i] &gt; playerA[i]) {
playerBScore = playerBScore + 5;
}
``````
1. Second mistake is probably that you are playing only `playerA.length` times. Which in this case is 10, so player gets 50 only in case he wins all of the games (which is very very low probability).

2. Number of rounds displayed at the end is wrong. The number of played rounds is `10` while it is incremented once more and states the number of rounds is `11`

# 答案2

``````    public static void main(String[] args) {
Game game = new Game();

game.round(50);

System.out.println("游戏结束。");

if (game.playerAScore >= game.playerBScore)
System.out.println("胜利者是玩家A");
else
System.out.println("胜利者是玩家B");

System.out.println("进行了多少轮游戏？");
System.out.println("已玩轮次: " + game.round);

System.out.println("每个玩家的总得分：");
System.out.println("玩家A得分: " + game.playerAScore);
System.out.println("玩家B得分: " + game.playerBScore);
}
``````
``````    static class Game {

private int playerAScore = 0;

private int playerBScore = 0;

private int round;

void round(int maximumScore) {
System.out.println("掷骰子进行第 " + round + " 轮");

int playerARoll = (int) ((Math.random() * 6) + 1);

int playerBRoll = (int) ((Math.random() * 6) + 1);

System.out.println("玩家A得到 " + playerARoll + " 点，玩家B得到 " + playerBRoll + " 点");

if (playerARoll == playerBRoll) {
playerAScore += 3;
playerBScore += 3;
} else if (playerARoll > playerBRoll) {
playerAScore += 5;
} else {
playerBScore += 5;
}
if (playerAScore >= maximumScore || playerBScore >= maximumScore) {
return;
}
round++;
round(maximumScore);
}
}
``````

``````掷骰子进行第 0 轮

``````

Other people have pointed out the flaw with `PlayerB &gt; PlayerB` however this answer is pointed towards how to fix the issue of only player 10 rounds. Instead of limiting to 10, allow a function to execute recursively until 50 score it met.

``````    public static void main(String[] args) {
Game game = new Game();

game.round(50);

System.out.println(&quot;The game is over.&quot;);

if(game.playerAScore &gt;= game.playerBScore)
System.out.println(&quot;The winner is player A&quot;);
else
System.out.println(&quot;The winner is player B&quot;);

System.out.println(&quot;How many rounds of game played?&quot;);
System.out.println(&quot;Rounds played: &quot; + game.round);

System.out.println(&quot;Total Score per player:&quot;);
System.out.println(&quot;Player A score: &quot; + game.playerAScore);
System.out.println(&quot;Player B score: &quot; + game.playerBScore);
}
``````
``````    static class Game {

private int playerAScore = 0;

private int playerBScore = 0;

private int round;

void round(int maximumScore) {
System.out.println(&quot;Roll the die for round &quot; + round);

int playerARoll = (int) ((Math.random() * 6) + 1);

int playerBRoll = (int) ((Math.random() * 6) + 1);

System.out.println(&quot;player A has &quot; + playerARoll + &quot; and player B has &quot; + playerBRoll);

if (playerARoll == playerBRoll) {
playerAScore += 3;
playerBScore += 3;
} else if (playerARoll &gt; playerBRoll) {
playerAScore += 5;
} else {
playerBScore += 5;
}
if(playerAScore &gt;= maximumScore || playerBScore &gt;= maximumScore) {
return;
}
round++;
round(maximumScore);
}
}
``````

Output

``````Roll the die for round 0
player A has 2 and player B has 2
Roll the die for round 1
player A has 2 and player B has 2
Roll the die for round 2
player A has 6 and player B has 6
Roll the die for round 3
player A has 6 and player B has 6
Roll the die for round 4
player A has 6 and player B has 3
Roll the die for round 5
player A has 5 and player B has 2
Roll the die for round 6
player A has 2 and player B has 2
Roll the die for round 7
player A has 3 and player B has 1
Roll the die for round 8
player A has 2 and player B has 4
Roll the die for round 9
player A has 6 and player B has 6
Roll the die for round 10
player A has 1 and player B has 4
Roll the die for round 11
player A has 2 and player B has 3
Roll the die for round 12
player A has 2 and player B has 5
Roll the die for round 13
player A has 6 and player B has 4
Roll the die for round 14
player A has 1 and player B has 2
Roll the die for round 15
player A has 1 and player B has 5
Roll the die for round 16
player A has 3 and player B has 4
The game is over.
The winner is player B
How many rounds of game played?
Rounds played: 16
Total Score per player:
Player A score: 38
Player B score: 53
``````

# 答案3

Here is the translated code snippet:

``````else if (playerB[i] &gt; playerA[i]) playerBScore = playerBScore + 5;
}
``````

``````public class diceGame {
public static void main(String[] args) {
int playerA[] = new int[10];
int playerB[] = new int[10];
int playerAScore = 0;
int playerBScore = 0;
int round = 1;

for (int i = 0; i < playerA.length; i++) {
System.out.println("Roll the die for round " + round++);
playerA[i] = (int) ((Math.random() * 6) + 1);
playerB[i] = (int) ((Math.random() * 6) + 1);

System.out.println("player A has " + playerA[i] + " and player B has " + playerB[i]);

if (playerA[i] == playerB[i]) {
playerAScore = playerAScore + 3;
playerBScore = playerBScore + 3;
} else if (playerA[i] > playerB[i]) {
playerAScore = playerAScore + 5;
} else if (playerB[i] > playerA[i]) {
playerBScore = playerBScore + 5;
}
if (playerAScore >= 50 || playerBScore >= 50) {
break;
}
}

System.out.println("The game is over.");

if (playerAScore >= playerBScore)
System.out.println("The winner is player A");
else
System.out.println("The winner is player B");

System.out.println("How many rounds of game played?");
System.out.println("Rounds played: " + round);

System.out.println("Total Score per player:");
System.out.println("Player A score: " + playerAScore);
System.out.println("Player B score: " + playerBScore);

}
}
``````

It looks like you just made a typo in the line:

``````else if (playerB[i] &gt; playerA[i]) playerBScore = playerBScore + 5;
}
``````

Here is the amended code:

``````public class diceGame{
public static void main(String[] args) {
int playerA[] = new int[10];
int playerB[] = new int[10];
int playerAScore = 0;
int playerBScore = 0;
int round = 1;
for (int i =0; i &lt; playerA.length; i++) {
System.out.println(&quot;Roll the die for round &quot; + round++);
playerA[i] = (int) ((Math.random() * 6) + 1);
playerB[i] = (int) ((Math.random() * 6) + 1);
System.out.println(&quot;player A has &quot; + playerA[i] + &quot; and player B has &quot; + playerB[i]);
if(playerA[i] == playerB[i]) {
playerAScore = playerAScore + 3;
playerBScore = playerBScore + 3;
}
else if (playerA[i] &gt; playerB[i]) {
playerAScore = playerAScore + 5;
}
else if (playerB[i] &gt; playerA[i]) {
playerBScore = playerBScore + 5;
}
if(playerAScore &gt;= 50 || playerBScore &gt;= 50) {
break;
}
}
System.out.println(&quot;The game is over.&quot;);
if(playerAScore &gt;= playerBScore)
System.out.println(&quot;The winner is player A&quot;);
else
System.out.println(&quot;The winner is player B&quot;);
System.out.println(&quot;How many rounds of game played?&quot;);
System.out.println(&quot;Rounds played: &quot; + round);
System.out.println(&quot;Total Score per player:&quot;);
System.out.println(&quot;Player A score: &quot; + playerAScore);
System.out.println(&quot;Player B score: &quot; + playerBScore);
}
}
``````

# 答案4

"Instead of a `for` loop you should use `while`. In your case one of the players have to win every time to score 50 points. Try this: `while (playerAScore &lt; 50 &amp;&amp; playerBScore &lt; 50)` and increment index `i` inside the loop."

"不要使用 `for` 循环，应该使用 `while` 循环。在你的情况下，一名玩家每次都必须获胜以得分50分。尝试这样做：`while (playerAScore &lt; 50 &amp;&amp; playerBScore &lt; 50)`，并在循环内增加索引 `i`。"

Instead of a `for` loop you should use `while`. In your case one of the players have to win every time to score 50 points. Try this: `while (playerAScore &lt; 50 &amp;&amp; playerBScore &lt; 50)` and increment index `i` inside the loop.

• 本文由 发表于 2020年7月29日 06:04:09
• 转载请务必保留本文链接：https://go.coder-hub.com/63143459.html
• arrays
• java
• random

go 50

go 78

go 42

go 60