英文:
Dice game between 2 players up to a score of 50
问题
我正在创建一个游戏,在这个游戏中,两名玩家互相掷骰子。
两个人(玩家A、玩家B)玩一个骰子游戏。每一轮他们都掷一次骰子,点数更高的人赢得比赛,并赚取5分。如果两人掷骰子的点数相同,他们都会赚取3分。游戏结束的条件是其中一名玩家达到50分或更高分数。如果两名玩家在同一轮达到50分,那就是平局,还需要进行一轮游戏,直到点数更高的玩家获胜(如果第11轮仍然平局,则继续下一轮)。以下是我目前的代码,但我无法使得分达到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 < 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);
}
}
答案1
得分: 1
- 在比分比较中,您有一个拼写错误。
playerB[i] > playerB[i]
这总是为false,所以一个玩家根本不会得到他的分数。
playerBScore = playerBScore + 5;
}
这部分显然应该是:
playerBScore = playerBScore + 5;
}
-
第二个错误可能是您只玩了
playerA.length
次。在这种情况下是10,所以只有在玩家赢得所有比赛的情况下,他才能得到50分(这个概率非常非常低)。 -
结尾显示的回合数是错误的。已经玩了10轮,但增加了一轮,所以显示的回合数是
11
。
英文:
- In the comparison between scores you have a typo.
playerB[i] > playerB[i]
which is always false, so one player doesn't get his points at all.
else if (playerB[i] > playerB[i]) {
playerBScore = playerBScore + 5;
}
fragment. Which obviously should be:
else if (playerB[i] > playerA[i]) {
playerBScore = playerBScore + 5;
}
-
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). -
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 is11
答案2
得分: 1
其他人已经指出了PlayerB > PlayerB
的问题,但这个答案是关于如何解决只进行10轮比赛的问题的。不要限制为10轮,允许一个函数递归执行,直到达到50分。
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 轮
玩家A得到 2 点,玩家B得到 2 点
掷骰子进行第 1 轮
玩家A得到 2 点,玩家B得到 2 点
掷骰子进行第 2 轮
玩家A得到 6 点,玩家B得到 6 点
掷骰子进行第 3 轮
玩家A得到 6 点,玩家B得到 6 点
掷骰子进行第 4 轮
玩家A得到 6 点,玩家B得到 3 点
掷骰子进行第 5 轮
玩家A得到 5 点,玩家B得到 2 点
掷骰子进行第 6 轮
玩家A得到 2 点,玩家B得到 2 点
掷骰子进行第 7 轮
玩家A得到 3 点,玩家B得到 1 点
掷骰子进行第 8 轮
玩家A得到 2 点,玩家B得到 4 点
掷骰子进行第 9 轮
玩家A得到 6 点,玩家B得到 6 点
掷骰子进行第 10 轮
玩家A得到 1 点,玩家B得到 4 点
掷骰子进行第 11 轮
玩家A得到 2 点,玩家B得到 3 点
掷骰子进行第 12 轮
玩家A得到 2 点,玩家B得到 5 点
掷骰子进行第 13 轮
玩家A得到 6 点,玩家B得到 4 点
掷骰子进行第 14 轮
玩家A得到 1 点,玩家B得到 2 点
掷骰子进行第 15 轮
玩家A得到 1 点,玩家B得到 5 点
掷骰子进行第 16 轮
玩家A得到 3 点,玩家B得到 4 点
游戏结束。
胜利者是玩家B
进行了多少轮游戏?
已玩轮次: 16
每个玩家的总得分:
玩家A得分: 38
玩家B得分: 53
英文:
Other people have pointed out the flaw with PlayerB > 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("The game is over.");
if(game.playerAScore >= game.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: " + game.round);
System.out.println("Total Score per player:");
System.out.println("Player A score: " + game.playerAScore);
System.out.println("Player B score: " + game.playerBScore);
}
static class Game {
private int playerAScore = 0;
private int playerBScore = 0;
private int round;
void round(int maximumScore) {
System.out.println("Roll the die for round " + round);
int playerARoll = (int) ((Math.random() * 6) + 1);
int playerBRoll = (int) ((Math.random() * 6) + 1);
System.out.println("player A has " + playerARoll + " and player B has " + 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);
}
}
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
得分: 1
Here is the translated code snippet:
看起来你在这行中刚刚犯了一个拼写错误:
else if (playerB[i] > 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] > 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 < 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);
}
}
答案4
得分: 0
不要有别的内容,只返回翻译好的部分:
"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 < 50 && playerBScore < 50)
and increment index i
inside the loop."
应翻译为:
"不要使用 for
循环,应该使用 while
循环。在你的情况下,一名玩家每次都必须获胜以得分50分。尝试这样做:while (playerAScore < 50 && playerBScore < 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 < 50 && playerBScore < 50)
and increment index i
inside the loop.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论