如何使if语句查看数组是否具有特定的数字和字符并返回布尔值

时间:2019-05-10 10:39:45

标签: java arrays poker

我正在编写一个代码,以查找用户获得的卡。返回其是否为RoyalFlush。

我有一些代码可以找到RoyalFlush,但无法正常工作。

这是我的代码;

{
    private Card [] hand;        // the hand of 5 cards

    // the default constructor
    public PokerHand ()
    {
        hand = new Card [5];
    }

    // A constructor to help with testing
    public PokerHand (Card c0, Card c1, Card c2, Card c3, Card c4)
    {
        hand = new Card [5];
        hand[0] = c0;
        hand[1] = c1;
        hand[2] = c2;
        hand[3] = c3;
        hand[4] = c4;
    }

    // This methods fills the hand with cards from the deck.
    // It uses an insertion sort so that the cards are ordered by rank.
    public void fillHand (Deck deck)
    {
        for (int i=0; i<5; i++)
        {
            int j=i-1;
            Card temp = deck.dealCard();
            while (j>=0 && hand[j].getRank() > temp.getRank())
            {   
                hand[j+1] = hand[j];
                j--;
            }
            hand[j+1] = temp;
        }
    }

    // PLACE ADDITIONAL METHODS AFTER THIS COMMENT
    public boolean isRoyalFlush()
    {
        boolean royalFlush = false;
        // simplify the name of hand
        Card c0 = hand[0];
        Card c1 = hand[1];
        Card c2 = hand[2];
        Card c3 = hand[3];
        Card c4 = hand[4];
        // if the five cards in this hand have the same suit, it is flush
        if((c0.getSuit() == c1.getSuit()) && (c1.getSuit() == c2.getSuit())
        && (c2.getSuit() == c3.getSuit()) && (c3.getSuit() == c4.getSuit()))
        {
            // if the five cards in this hand consists of ace, king, queen, jack 
            // and ten, it is royal
            if((c0.getRank() == 10) && (c1.getRank() == 11) && (c2.getRank() == 12)
            && (c3.getRank() == 13) && (c4.getRank() == 14))
             royalFlush = true;
        }
        return royalFlush;
    }

在另一堂课上,我将需要统计有多少用户获得了RoyalFlush。

if(hand.isRoyalFlush())
                count[0]++;

但计数始终显示为0。

1 个答案:

答案 0 :(得分:0)

因为您已经检查了具有特定等级的特定卡。每个人只有一张等级。皇家同花顺在哪里可以是任何命令。您要检查的是第一张卡是10,第二张是插孔,依此类推。但是在某些情况下,第一张卡的第二张为10,依此类推,这将是皇家同花顺。

解决方案-检查皇家同花顺之前,根据排名对卡列表进行排序