0

试图让这些算法来评估一个数组,该数组填充了五个随机选择的整数,范围从 1 到 6。不幸的是,它只会返回高牌和一对选项。当我掷出两对、三对等时,如何获得更高的分数?

private static int[] getCounts(int[] dice) {

    int[] counts = new int[6];
    String resValue = "";
    for (int i = 0; i < dice.length; i++) {
        if (dice[i] == 1) {
            counts[0]++;
        } else if (dice[i] == 2) {
            counts[1]++;
        } else if (dice[i] == 3) {
            counts[2]++;
        } else if (dice[i] == 4) {
            counts[3]++;
        } else if (dice[i] == 5) {
            counts[4]++;
        } else if (dice[i] == 6) {
            counts[5]++;
        }
    }
    return counts;
}

private static String getResult(int[] dice) {
    int[] counts = getCounts(dice);
    String resValue = " ";

    for (int i = 0; i < counts.length; i++) {
        if (counts[i] == 5) {
            resValue = "Five of a kind ";
        } else if (counts[i] == 4) {
            resValue = "Four of a kind ";
        } else if (counts[i] == 3) {
            for (int j = 0; j < counts.length; j++) {
                if (counts[j] == 2) {
                    resValue = "Full House ";
                }
            }
            resValue = "Three of a Kind ";
        } else if (counts[i] == 2) {
            for (int j = 0; j < counts.length; j++) {
                if (counts[j] == 2) {
                    resValue = "Two Pairs ";
                }
            }
            resValue = "One Pair ";
        } else {
            resValue = "Highest Card ";
        }
    }
    return resValue;
}
4

1 回答 1

0

你的 if else 范围很广。即使我们之前发现更高的 resValue,最后一个 else 也会执行。添加检查以查看新的 resValue 是否更高,或者在每个子句的末尾添加 break。

编辑:在这里,它对我有用:

private static int[] getCounts(int[] dice) {
        int[] counts = new int[6];
        String resValue = "";
        for (int i = 0; i < dice.length; i++) {
            if (dice[i] == 1) {
                counts[0]++;
            } else if (dice[i] == 2) {
                counts[1]++;
            } else if (dice[i] == 3) {
                counts[2]++;
            } else if (dice[i] == 4) {
                counts[3]++;
            } else if (dice[i] == 5) {
                counts[4]++;
            } else if (dice[i] == 6) {
                counts[5]++;
            }
        }
        return counts;
    }

    private static String getResult(int[] dice) {
        int[] counts = getCounts(dice);
        String resValue = " ";

        for (int i = 0; i < counts.length; i++) {
            if (counts[i] == 5) {
                resValue = "Five of a kind ";
                break;
            } else if (counts[i] == 4) {
                resValue = "Four of a kind "; break;
            } else if (counts[i] == 3) {
                resValue = "Three of a Kind ";
                for (int j = 0; j < counts.length; j++) {
                    if (counts[j]==2) {
                        resValue = "Full House "; break;
                    }
                }
                break;
            } else if (counts[i] == 2) {
                 resValue = "One Pair ";
                for (int j = 0; j < counts.length; j++) {
                    if (counts[j] == 2) {
                        resValue = "Two Pairs "; break;
                    }
                }
               break;
            } else {
                resValue = "Highest Card ";
            }
        }
        return resValue;
    }
于 2013-03-25T05:51:23.430 回答