所以我在 BlackJack 的 Java 游戏中遇到了一些麻烦。我已经从对卡片名称和值使用哈希映射并将其转换为 String[] 只是因为它更容易。但是,我似乎破坏了其中的一些代码:
private void createDeck(int numCards, int numSuits) {
deck = new ArrayList<Card>();
cardUsed = new ArrayList<Card>();
if ((numCards % numSuits) > 0) return;
for (int i=0; i < numSuits; i++) {
for(int j=1; j <= (numCards / numSuits); j++) {
deck.add(new Card(new Suit(i), j + "", j));
}
}
}
这不会运行并给我一个未解决的编译问题:构造函数 Card(Suit, String, int) 未定义。但是我有 Card 的参数为 Card(Suit, String[], int)。有没有办法通过仍然使用字符串数组来解决这个问题?它强调了deck.add(new Card(new Suit(i), j + "", j));
程序中的 。
这是 Card.java 的源代码
public class Card {
private String[] cardRank = new String[] {"Ace", "Two", "Three", "Four",
"Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"} ;
private Suit suit = null;
private int cardValue = 0;
public Card(Suit suit, String[] cardRank, int cardValue) {
this.cardRank = cardRank;
this.suit = suit;
this.cardValue = cardValue;
}
public String toString() {
return cardRank + " of " + suit.getSuitName();
}
public String[] getCardRank() {
return cardRank;
}
public void setCardRank(String[] cardRank) {
this.cardRank = cardRank;
}
public Suit getSuit() {
return suit;
}
public void setSuit(Suit suit) {
this.suit = suit;
}
public int getCardValue() {
return cardValue;
}
public void setCardValue(int cardValue) {
this.cardValue = cardValue;
}
}