为什么我的阵列为一副纸牌建模返回空纸牌对象?

夏日光芒:

所以这是我的套牌课程

/**
Constructs a deck with 52 cards
*/
public Deck() {

    int k = 0;  // counter to keep track of elements in the deck array

    // nested for loops to populate the deck of cards with 4 suits and 13 possible rankings
    for (int i = 1; i < SUITS; i++) {
        for (int j = 1; j < RANKS; j++) {
            deckOfCards[k] = new Card(i, j);  // adds the cards to the deck array 
            k++;                              // increment the elements counter by 1
            System.out.println(deckOfCards[k]);
        }
    }

}

这是我的卡片课程,即使我几乎是肯定的,这部分也没有任何问题

public class Card {

    private int rank;
    private int suit;

    /**
    * @param suit the suit of the card in a deck 
    * @param rank the rank of the card in a deck 
    */
    public Card(int suit, int rank) {

        this.rank = rank;       // initializing the rank 
        this.suit = suit;       // initializing the suit
    }

当我打印出卡片组中的卡片时,我会被null退回。有什么想法吗?

迈克尔·比安科尼(Michael Bianconi):
deckOfCards[k] = new Card(i, j);
k++;
System.out.println(deckOfCards[k]);

您正在设置deckOfCards[k]正在打印deckOfCards[k+1]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章