在switch语句中使用非常量表达式?

时间:2015-02-23 16:32:07

标签: java

我对java很陌生并创建了一个创建一副牌的Java类,并为它们分配套件,名称,值和ID。问题是,有52种不同的卡需要ID,我一直在使用switch语句来分配名称,值和套件。但是,如果我要为卡ID做这个,我需要52行案例陈述,这太多了。

public class Deck {

    private Card[] cards;

    public Deck() {

        String suit = null;
        String name = null;  
        int cardID=0;
        int value = 0;
        cards = new Card[52];

        int arrayID=0;

        for (int i=1; i<=4; i++){ //number of suites
            for (int j=1; j <= 13; j++){ //number of card types
                for (int k=1; k==52; k++){ //number of cards

                    switch (i){
                    case 1: suit = "Clubs"; break;
                    case 2: suit = "Diamonds"; break;
                    case 3: suit = "Hearts";  break;
                    case 4: suit = "Spades"; break;
                    }

                    switch (j){
                    case 1: name = "Ace"; value = 11; break;
                    case 2: name = "Two"; value = 2; break;
                    case 3: name = "Three"; value = 3; break;
                    case 4: name = "Four"; value =4; break;
                    case 5: name = "Five"; value = 5; break;                             
                    case 6: name = "Six"; value = 6; break;
                    case 7: name = "Seven"; value = 7; break;
                    case 8: name = "Eight"; value = 8; break;
                    case 9: name = "Nine"; value = 9; break;
                    case 10: name = "Ten"; value = 10; break;                           
                    case 11: name = "Jack"; value = 10; break;
                    case 12: name = "Queen"; value = 10; break;
                    case 13: name = "King"; value = 10; break;                           
                    }
                    switch (k){
                    case k: cardID=k; break; //"Case expressions must be constant expressions"

                    }
                    Card card = new Card (cardID, name, suit, value);  
                    cards[arrayID] = card; 
                    arrayID++;
                }
            }
        }
    }

    public void printDeck(){
        System.out.println(Arrays.toString(cards));
    }

}

我可能做错了,所以有没有其他方法可以在不使用switch语句的情况下为卡分配唯一的ID?

4 个答案:

答案 0 :(得分:3)

此切换声明中没有任何意义:

            switch (k){
            case k: cardID=k; break; //"Case expressions must be constant expressions"

            }

只需写下:

cardID = k;

答案 1 :(得分:0)

您可以使用枚举,并且您可以使用序数值自动获取每个计数,并且您可以在您的案例中拥有类似值的属性,您可以这样做:

public enum Cards {
    ACE(11), TWO(..), .. JACK(....;
    int value;
    public int getValue() {return value;}
}

System.out.println(CARDS.ACE.ordinal() + 1);
System.out.println(CARDS.ACE.getValue());
Output:
1
11

答案 2 :(得分:0)

您不需要cardID的循环或switch语句。您可以从ij

进行综合
for (int i=1; i<=4; i++){ //number of suites
        for (int j=1; j <= 13; j++){ //number of card types
                cardId = (i - 1) * 13 + j; // <<< synthesize cardID like this
                switch (i){
                case 1: suit = "Clubs"; break;
                case 2: suit = "Diamonds"; break;
                case 3: suit = "Hearts";  break;
                case 4: suit = "Spades"; break;
                }

                switch (j){
                case 1: name = "Ace"; value = 11; break;
                case 2: name = "Two"; value = 2; break;
                case 3: name = "Three"; value = 3; break;
                case 4: name = "Four"; value =4; break;
                case 5: name = "Five"; value = 5; break;                             
                case 6: name = "Six"; value = 6; break;
                case 7: name = "Seven"; value = 7; break;
                case 8: name = "Eight"; value = 8; break;
                case 9: name = "Nine"; value = 9; break;
                case 10: name = "Ten"; value = 10; break;                           
                case 11: name = "Jack"; value = 10; break;
                case 12: name = "Queen"; value = 10; break;
                case 13: name = "King"; value = 10; break;                           
                }
                Card card = new Card (cardID, name, suit, value);  
                cards[arrayID] = card; 
                arrayID++;
            }
        }

答案 3 :(得分:0)

我会做的是:

创建一张类卡:

class Card {
    private String suit;
    private String name;
    private int ID;

    public Card(String suit, String name, int ID) {
        this.suit = suit
        (same for name & ID)}}

然后,在您的Deck课程中,您可以拥有一个包含所有卡片的ArrayList,并使用一些方法在您的套牌中添加卡片:

s
class Deck {
    private ArrayList<Cards> myDeck = new ArrayList<Cards>();

    public void addCards(Card myCard) {
        this.myDeck.add(myCard); } }

编辑:忘记addCards中的变量类型