【发布时间】:2018-11-08 09:42:24
【问题描述】:
[编辑:我修复了两对和满堂彩的混淆,但是我的最终得分在没有对子并且显示最高值的情况下根本没有触发]
我正在做一个玩骰子扑克游戏的项目。除了骰子掷出两对[编辑:固定两对]时,我所有的“得分”都在工作,它说它是满堂彩。
以下是说明:
在本实验中,您将编写一个玩扑克骰子游戏的 Java 程序。在这个游戏中,五个骰子被滚动并得分,就好像它们是一手扑克牌一样。游戏经历了两个不同的阶段。在第一阶段,向玩家呈现骰子“手”。然后玩家选择他想要保留的骰子以及他想要重新掷的骰子。一旦该决定完成,玩家标记为重新掷骰子的所有骰子都会重新掷骰子,并显示最后的骰子“手”。然后应根据扑克骰子的规则(如下所示)对手牌进行评分,结果将显示在屏幕上。
这是我的评分方法代码:
private static String getResult(int[] dice) {
int i = 0;
String results = "";
int endResultsCheck = 0;
int[] count = getCounts(dice);
// Checks for 5 of a kind
while (i < count.length) {
if (count[i] == 5) {
results = "Five of a kind!";
endResultsCheck = 7;
}
i++;
}
// Checks for four of a kind
i = 0;
if (endResultsCheck != 7) {
while (i < count.length) {
if (count[i] == 4) {
results = "Four of a kind!";
endResultsCheck = 6;
}
i++;
}
}
if (endResultsCheck < 6) {
// Checks for full house
i = 0;
int check = 0;
while (i < count.length) {
if (count[i] == 3) {
check++;
}
if (count[i] == 2) {
check++;
}
i++;
}
if (check == 2) {
results = "Full house!";
endResultsCheck = 5;
}
}
if (endResultsCheck < 5) {
// Checks for three of a kind
i = 0;
while (i < count.length) {
if (count[i] == 3) {
results = "Three of a kind!";
endResultsCheck = 4;
}
i++;
}
}
if (endResultsCheck < 4) {
// Checks for two pairs
i = 0;
int check = 0;
while (i < count.length) {
if (count[i] == 2) {
check++;
}
i++;
}
if (check == 2) {
results = "Two pairs!";
endResultsCheck = 3;
}
}
if (endResultsCheck < 3) {
i = 0;
while (i < count.length) {
if (count[i] == 2) {
results = "One pair!";
}
i++;
}
endResultsCheck = 2;
}
if (endResultsCheck == 0) {
i = 0;
int max = 0;
while (i < dice.length) {
if (max < dice[i]) {
max = dice[i];
}
i++;
}
results = "Highest number is " + max + "!";
}
return results;
}
当它引用方法getCounts()时,是这样的代码:
private static int[] getCounts(int[] dice) {
int[] count = new int[10];
int i = 0;
int one = 0;
while (i < dice.length) {
if (dice[i] == 1) {
one++;
}
i++;
}
int two = 0;
i = 0;
while (i < dice.length) {
if (dice[i] == 2) {
two++;
}
i++;
}
int three = 0;
i = 0;
while (i < dice.length) {
if (dice[i] == 3) {
three++;
}
i++;
}
int four = 0;
i = 0;
while (i < dice.length) {
if (dice[i] == 4) {
four++;
}
i++;
}
int five = 0;
i = 0;
while (i < dice.length) {
if (dice[i] == 5) {
five++;
}
i++;
}
int six = 0;
i = 0;
while (i < dice.length) {
if (dice[i] == 6) {
six++;
}
i++;
}
int seven = 0;
i = 0;
while (i < dice.length) {
if (dice[i] == 7) {
seven++;
}
i++;
}
int eight = 0;
i = 0;
while (i < dice.length) {
if (dice[i] == 8) {
eight++;
}
i++;
}
int nine = 0;
i = 0;
while (i < dice.length) {
if (dice[i] == 9) {
nine++;
}
i++;
}
int ten = 0;
i = 0;
while (i < dice.length) {
if (dice[i] == 10) {
ten++;
}
i++;
}
count[0] = one;
count[1] = two;
count[2] = three;
count[3] = four;
count[4] = five;
count[5] = six;
count[6] = seven;
count[7] = eight;
count[8] = nine;
count[9] = ten;
return count;
}
【问题讨论】:
-
你尝试调试了吗?
-
检查
endResultsCheck = 2;在if (endResultsCheck < 3)块中,你应该把它移到if (count[i] == 2)块中 -
提示:如果它没有进入你想要的
if块,你可以随时检查它之前的条件。例如在这种情况下,我在if (endResultsCheck == 0)块之前添加了一行System.out.println(endResultsCheck);,结果是 2,然后我知道我应该查看 endResultsCheck 设置为 2 的循环。