简体   繁体   English

我该如何声明西服套装Diamond and Hearts?

[英]How can I declare the suits Diamond and Hearts?

Okay in the code below I want to declare that "Diamonds" and "Hearts" are red. 好的,在下面的代码中,我想声明“钻石”和“心脏”为红色。

Code Here 在这里编码

if (card.getCardFace() == || card.getCardFace() == )
            g.setColor(Color.red);
         else
           g.setColor(Color.black);

I was wondering on how I could do that if I declared them as chars in my Card class. 我想知道如果我在Card类中将它们声明为chars怎么办。 Do I have to change that ? 我需要更改吗?

Card Class

// Instance Data - all things common to all cards
private String cardFace; // king, q, j, 10 - 2, A
private int faceValue; // numberic value of the card
private char cardSuit; // hold suit of the card
private char suits[] = {(char)(003), (char)(004), (char)(005), (char)(006)};

// Constructor
public PlayingCard(int value, int suit)
{
    faceValue = value;
    setFace();
    setSuit(suit);
}

// helper setFace()
public void setFace()
{
    switch(faceValue)
    {
        case 1:
            cardFace = "A";
            faceValue = 14;
            break;
        case 11:
            cardFace = "J";
            break;
        case 12:
            cardFace = "Q";
            break;
        case 0:
            cardFace = "K";
            faceValue = 13;
            break;
        default:
            cardFace = ("" + faceValue);
    }
}

public void setSuit(int suit) // suit num between 0 and 3
{
    cardSuit = suits[suit];
}

// other helpers
public int getFaceValue()
{
    return faceValue;
}
public String getCardFace()
{
    return cardFace;
}

public String toString()
{
    return (cardFace + cardSuit);
}
}

(Also let me know if you need more code) (也请告知我是否需要更多代码)

You should use Enums to define types for ranks and suits, something like: 您应该使用Enums来定义等级和西服的类型,例如:

public static enum Suit {
    CLUB, DIAMOND, SPADE, HEART
}

public static enum Rank {
    TWO("2"), THREE("3"), FOUR("4"), FIVE("5"), SIX("6"), SEVEN("7"), 
    EIGHT("8"), NINE("9"), TEN("10"), JACK("J"), QUEEN("Q"), KING("K"), 
    ACE("A");

    private final String symbol;

    Rank(String symbol) {
        this.symbol = symbol;
    }

    public String getSymbol() {
        return symbol;
    }
}

---

class Card {
    Suit suit;
    Rank rank;
}

Then just compare the values: 然后只需比较这些值:

if (card.suit == Suit.DIAMONDS || card.suit == Suit.HEARTH) {
    color = Color.RED;
} else {
    color = Color.BLACK;
}

Or define a color as a property of Suit and then just use: 或将颜色定义为Suit的属性,然后使用:

color = card.suit.getColor();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM