无法创建Card类

时间:2015-03-17 01:01:00

标签: java

我无法将r [rank]转换为String。 Eclipse一直说它需要解析为一个数组。卡也有问题[13 * i + j] =新卡(i,j);它只是说不能解决变量。继承我的代码......

public class Card {

    private String suit;
    private String rank;
    private static String[] s = { "hearts", "spades", "diamonds", "clubs" };
    private static String[] r  = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" };

    public Card(int suit, int deck){
        for(int i = 0; i < 4; i++) {
            for(int j = 0; j < 13; j++){
               Cards[13*i + j] = new Card(i, j); 
            }
        }
    }

    public Card(String r, String s, String suit){
        for(int rank = 0; rank < r.length(); rank+=1){
            if( rank.equals(r[rank]) ){
                this.rank = r+1;
            }
        }
        this.suit = suit;
    }

    public String getSuit(){
        return suit;
    }

    public void setSuit(String a, String suit) {
        this.suit = suit;
    }

    public String getRank(){
        return rank;
    }
    public void setRank( String s, String rank){
        this.rank= rank;
    }

    public String toString(){
        return String.valueOf(this.getRank()) + String.valueOf(this.getSuit().charAt(0));
    }

}

公共课甲板{

private Card[] deck;
private int inDeck;
private final int SIZE_OF_DECK = 52; 
private int currentCard;

public Deck(){
    deck = new Card[deck];
}

private void init(){

}

public void shuffle(int num){
    int i,j,k;
    for (k = 0; k < num; k++)
    {
        i = (int) (SIZE_OF_DECK * Math.random()*100);
        j = (int) (SIZE_OF_DECK * Math.random()*100);

        Card tmp = deck[i];
        deck[i] = deck[j];
        deck[j] = tmp;
    }
    currentCard = 0;
}

public Card getCard(){
    if (currentCard<SIZE_OF_DECK)
    {
        return deck[currentCard++];
    }
    else
    {
        System.out.print("out of cards error");
        return null;
    }
    }

public int totalCards(){
    return SIZE_OF_DECK ;}

}

1 个答案:

答案 0 :(得分:1)

你永远不会初始化你的卡片阵列...在用你的循环创建每个单独的对象之前尝试一下这个行。

Card[] cards = new Card[52];