我正在尝试构建二十一点游戏。我已经创建了一个套牌,现在我想展示它。
我知道我做错了什么,因为我无法在 CardStack 类中访问 displayStack。另外,我有一种感觉,我没有正确地进行继承。我该如何解决这个问题?
这是我的代码:
public class CreateCardDeck {
int deckSize = 52;
CardStack cardStack = new CardStack(deckSize);
public void CreateDeck() {
String[] suit = {"clubs", "diamonds", "hearts", "spades"};
int[] rank = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
for (int i = 0; i < rank.length; i ++) {
for (int j = 0; j < suit.length; j++) {
cardStack.push(suit[j], rank[i]);
}
}
}
}
卡类:
class Card {
String suit;
int rank;
Card(String suit, int rank) {
this.suit = suit;
this.rank = rank;
}
public String getSuit() {
return suit;
}
public String getRank() {
String nameTheRank;
if (rank == 1)
nameTheRank = "Ace";
else if (rank == 11)
nameTheRank = "Jack";
else if (rank == 12)
nameTheRank = "Queen";
else if (rank == 13)
nameTheRank = "King";
else
nameTheRank = String.valueOf(rank);
return nameTheRank;
}
}
CardStack 类:
class CardStack {
public void displayDeck() {
for (int i = 0; i < stackArray.length; i ++)
System.out.println(stackArray[i]);
}
}
主类:
public class MainClass {
public static void main(String[] args) throws IOException {
CreateCardDeck c = new CreateCardDeck();
c.CreateDeck();
// How to display my deck?
}
}