使用二维数组在java中创建一副牌

时间:2016-09-03 23:10:01

标签: java arrays

我正在尝试创建一个套牌并为每个套件应用套件,例如:

cardValue[0][1]并将其转换为cardValue[Spade][1]

但我不明白如何转换它。我目前所拥有的:

        int cardValues[][];
        for(int a = 0; a <= 3; a++)
        {
            for(int b = 0; b <= 12; b++)
            {
                cardValues[][] += cardValues[a][b];
                if(cardValues[a][b] == cardValues[a][0])
                {
                    cardValues[a][b] = 5;
                }
                else if(cardValues[a][b] > cardValues[a][0] || cardValues[a][b] < cardValues[a][10])
                {   
                    cardValues[a][b] = 1;
                }
                else if(cardValues[a][b] == cardValues[a][10])
                {
                    cardValues[a][b] = 2;
                }
                else if(cardValues[a][b] == cardValues[a][11])
                {
                    cardValues[a][b] = 3;
                }
                else if(cardValues[a][b] == cardValues[a][12])
                {
                    cardValues[a][b] = 4;
                }
            }
        }

我不明白如何将0,1,2和3转换成黑桃,钻石,球杆,心脏。

1 个答案:

答案 0 :(得分:2)

我会做那样的事情:

<强> Suit.java

public enum Suit {
    SPADES, DIAMONDS, CLUBS, HEARTS
}

<强> Card.java

public class Card {

    private Suit suit;
    private String value;

    public Card(Suit suit, String value) {
        this.suit = suit;
        this.value = value;
    }

    public Suit getSuit() {
        return suit;
    }
    public void setSuit(Suit suit) {
        this.suit = suit;
    }
    public String getValue() {
        return value;
    }
    public void setValue(String value) {
        this.value = value;
    }

}

<强> Main.java

public class Main {

    public static void main(String[] args) {

        String[] values = new String[]{"K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3", "2", "A"};

        Card[] deck = new Card[52];

        int i = 0;
        for (Suit suit : Suit.values()) {
            for (String value : values) {
                deck[i++] = new Card(suit, value);
            }
        }

        for (Card card : deck) {
            System.out.println(card.getValue() + " of " + card.getSuit());
        }

    }

}