所以这是我的甲板班级填充一副牌
/**
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]);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的卡片课程,尽管我几乎肯定这部分没有任何问题
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
}
Run Code Online (Sandbox Code Playgroud)
当我打印出一副牌中的牌时,我会被null退回。任何想法为什么?
deckOfCards[k] = new Card(i, j);
k++;
System.out.println(deckOfCards[k]);
Run Code Online (Sandbox Code Playgroud)
您正在设置deckOfCards[k]但正在打印deckOfCards[k+1].