java 中的扑克骰子 - 分数不正确
poker dice in java - not giving scores properly
我想在java中创建一个扑克骰子游戏,当玩家掷骰子时,我想让程序告诉结果和当前分数。但是,有一点不对劲。它没有给我正确的分数。例如,我将 Math Random 算法更改为始终给我 (1,1,1,1,1),所以结果为 50。不幸的是,它给了我 0。请问有什么帮助吗?谢谢
这是我的代码:
public class DiceGame {
public static int [] rollDice() {
int [] diceSide = new int[5];
Random diceRoller = new Random();
for (int i = 0; i<diceSide.length; i++) {
int roll = diceRoller.nextInt(1) + 1;
diceSide[i] = roll;
}
System.out.print(diceSide[0] + "" + diceSide[1] + "" + diceSide[2] + "" + diceSide[3] + "" + diceSide[4]);
return diceSide;
}
public static int getResult(int[] dice) {
int resValue = 0;
for (int i = 0; i < dice.length; i++) {
if (dice[i] == 5) {
resValue = 50;
} else if (dice[i] == 4) {
resValue = 40;
} else if (dice[i] == 3) {
resValue = 30;
}
}
System.out.print(resValue);
return resValue;
}
public static void main(String[] args) {
int player1=0;
int player2;
int player3;
int player4;
int player5;
player1 += getResult(rollDice());
}
}
Math.Random returns 介于 0 和 1 之间的值。
您必须将此值乘以您想要接收的最大值 - 1 并将 +1 添加到整个值。不要忘记将这整个东西转换为 (int)。
它应该看起来像这样:
(int) (Math.Random(5) + 1)
这样,您将获得 1 到 6 之间的值。
在 rollDice
中,您从 java.util.Random
中获取一个值。然而,the upper limit is excluded,所以你总是通过 diceRoller.nextInt(1) + 1
.
得到一个
一个整数数组很适合表示多个骰子,所以如果这是您想要的,您可以使用 diceRoller.nextInt(6) + 1
来设置每个骰子的值。如果你想要一个骰子,你只需要一个整数变量。
你正在做一些不同的事情。您正在创建从 0 到整数最大值的随机数。
diceRoller.nextInt(1)
这会创建一个从 0 到 1 的随机数。所以很少有 5 乘以 1。如果你想模拟它,你应该把一个作为赋值
那么,因为这段代码:
for (int i = 0; i < dice.length; i++) {
if (dice[i] == 5) {
resValue = 50;
} else if (dice[i] == 4) {
resValue = 40;
} else if (dice[i] == 3) {
resValue = 30;
}
}
表示如果 dice[i]
的最后一个值是 3、4 或 5,它会得到一个新值。我认为你正在尝试做的是这个(将整个数组的骰子值加十倍):
int resValue = 0;
for (int i = 0; i < dice.length; i++) {
resValue += dice[i] * 10;
}
在最后一种情况下,如果所有 5 个 direRoll 结果都是 1,您将得到 50。
我想在java中创建一个扑克骰子游戏,当玩家掷骰子时,我想让程序告诉结果和当前分数。但是,有一点不对劲。它没有给我正确的分数。例如,我将 Math Random 算法更改为始终给我 (1,1,1,1,1),所以结果为 50。不幸的是,它给了我 0。请问有什么帮助吗?谢谢
这是我的代码:
public class DiceGame {
public static int [] rollDice() {
int [] diceSide = new int[5];
Random diceRoller = new Random();
for (int i = 0; i<diceSide.length; i++) {
int roll = diceRoller.nextInt(1) + 1;
diceSide[i] = roll;
}
System.out.print(diceSide[0] + "" + diceSide[1] + "" + diceSide[2] + "" + diceSide[3] + "" + diceSide[4]);
return diceSide;
}
public static int getResult(int[] dice) {
int resValue = 0;
for (int i = 0; i < dice.length; i++) {
if (dice[i] == 5) {
resValue = 50;
} else if (dice[i] == 4) {
resValue = 40;
} else if (dice[i] == 3) {
resValue = 30;
}
}
System.out.print(resValue);
return resValue;
}
public static void main(String[] args) {
int player1=0;
int player2;
int player3;
int player4;
int player5;
player1 += getResult(rollDice());
}
}
Math.Random returns 介于 0 和 1 之间的值。 您必须将此值乘以您想要接收的最大值 - 1 并将 +1 添加到整个值。不要忘记将这整个东西转换为 (int)。 它应该看起来像这样:
(int) (Math.Random(5) + 1)
这样,您将获得 1 到 6 之间的值。
在 rollDice
中,您从 java.util.Random
中获取一个值。然而,the upper limit is excluded,所以你总是通过 diceRoller.nextInt(1) + 1
.
一个整数数组很适合表示多个骰子,所以如果这是您想要的,您可以使用 diceRoller.nextInt(6) + 1
来设置每个骰子的值。如果你想要一个骰子,你只需要一个整数变量。
你正在做一些不同的事情。您正在创建从 0 到整数最大值的随机数。
diceRoller.nextInt(1)
这会创建一个从 0 到 1 的随机数。所以很少有 5 乘以 1。如果你想模拟它,你应该把一个作为赋值
那么,因为这段代码:
for (int i = 0; i < dice.length; i++) {
if (dice[i] == 5) {
resValue = 50;
} else if (dice[i] == 4) {
resValue = 40;
} else if (dice[i] == 3) {
resValue = 30;
}
}
表示如果 dice[i]
的最后一个值是 3、4 或 5,它会得到一个新值。我认为你正在尝试做的是这个(将整个数组的骰子值加十倍):
int resValue = 0;
for (int i = 0; i < dice.length; i++) {
resValue += dice[i] * 10;
}
在最后一种情况下,如果所有 5 个 direRoll 结果都是 1,您将得到 50。