[编辑:我固定了两对和满满的房子,但是我的最终成绩是没有对,并且显示出最高的价值,根本没有触发]
我正在从事一个玩骰子扑克游戏的项目。除了骰子滚动两对时,我已经完成了所有“计分”工作。[编辑:两对固定]说是满屋子。
以下是说明:
在本实验中,您将编写一个玩扑克骰子游戏的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;
}
答案 0 :(得分:1)
首先尝试调试(使用实际的调试模式并放置断点或放置打印值)下面的代码部分。您的目标是在此代码块内的逻辑中找到裂痕。接下来尝试更新逻辑。
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 (count[i] == 2) {
条件将为真两次。结果,check == 2
为真,您将收到“满屋”。
一种可能的解决方案:
if (endResultsCheck < 6) {
// Checks for full house
i = 0;
boolean hasPair = false;
boolean hasTriplet = false;
while (i < count.length) {
if (count[i] == 3) {
hasTriplet = true;
}
if (count[i] == 2) {
hasPair = true;
}
i++;
}
if (hasPair && hasTriplet) {
results = "Full house!";
endResultsCheck = 5;
}
}