五手纸牌游戏,想不通,badugi & 四

five hand card game and couldn't figure out, badugi & four of a kind

我正试图在一手五张牌的扑克牌中找出四张。但它不起作用,也不知道为什么。

public boolean hasFourOfaKind(String hand) {
        int counter = 0;
        char x = 0;

        for (int i = 0; i < hand.length(); i++) 
        {
            if (i == 0) {
                x = hand.charAt(0);
            } else if (x == hand.charAt(i)) {
                counter++;

            }
        }
        if (counter >= 4) {
            return true;
        } else {
            return false;
        }
    }

这里有同样的问题,我正在尝试检查给定的四张牌是否是八度牌

    public boolean hasFourCardBadugi(String hand) {
        int diffcounter = 0;
        char badugi = 0;

        for (int i = 0; i < hand.length(); i++) {
            if (i == 0) {
                badugi = hand.charAt(0);
            } else if (badugi != hand.charAt(i)) {
                diffcounter++;
            }
        }
        if (diffcounter >= 10) {
            return true;
        } else {
            return false;
        }
    }

让我们看看你的 for 循环。

    for (int i = 0; i < hand.length(); i++) 
    {
        if (i == 0) {
            x = hand.charAt(0);
        } else if (x == hand.charAt(i)) {
            counter++;
        }
    }

在这部分

        if (i == 0) {
            x = hand.charAt(0);
        }

您将 x 设置为第一张牌。但你永远不会算那张牌。您需要添加:

        if (i == 0) {
            x = hand.charAt(0);
            counter++;
        }

当然这仍然有一个问题,它不会检测到四张牌与第一张牌(A 二二二二)不匹配的手牌,但你应该能够解决这个问题,因为基本错误已修复。一种方法是只涉及第二个循环。