我正在为一所学校的工作而工作,而且我有一个问题,在我的生活中我无法弄明白。
这只是一个简单的卡片游戏"分配我们基本上只是创建一副牌,洗牌和处理几张牌。
我的问题来自"交易" Ace,Jack,Queen和King的牌仍然是从11-13打出来的牌。我认为问题应该在setValue()下的Card类中,但它似乎一切都好。我将首先发布该课程,然后发布其余内容,以防您需要查看。
感谢您的帮助!!
P.S。我试图在书中查一下但是找不到答案...... BlackJack类是本书前一章的部分源代码,我无法弄清楚是否有必要有super();在构造函数那里?我认为只有在父类构造函数有参数时才需要这样做?任何帮助,如果它必须在那里,如果是这样,它做什么。
public class Card {
protected String suit;
protected int value;
protected String rank;
protected final int LOW_VALUE = 1;
protected final int HIGH_VALUE = 13;
public String getRank() {
return rank;
}
public int getValue() {
return value;
}
public String getSuit() {
return suit;
}
public void setSuit(String st) {
suit = st;
}
public void setValue(int val) {
if(val >= LOW_VALUE && val <= HIGH_VALUE) {
value = val;
}
else {
value = LOW_VALUE;
}
if(val == 1) {
rank = "Ace";
}
else if(val == 11) {
rank = "Jack";
}
else if(val == 12) {
rank = "Queen";
}
else if(val == 13) {
rank = "King";
}
else {
rank = Integer.toString(value);
}
}
}
CardGame Class
abstract public class CardGameFP {
int suitNum = 1;
int val = 1;
int player1;
int player2;
protected final int DECK_OF_CARDS = 52;
Card fullDeck[] = new Card[DECK_OF_CARDS];
protected final int LOW_VALUE = 1;
protected final int HIGH_VALUE = 13;
protected final int HIGH_SUIT = 4;
protected final int CARDS_IN_SUIT = 13;
public abstract void display();
public abstract void dealCards();
public CardGameFP() {
for(int i = 0; i < fullDeck.length; i++) {
fullDeck[i] = new Card();
if(suitNum == 1) {
fullDeck[i].setSuit("Spades");
}
else if(suitNum == 2) {
fullDeck[i].setSuit("Hearts");
}
else if(suitNum == 3) {
fullDeck[i].setSuit("Diamonds");
}
else {
fullDeck[i].setSuit("Clubs");
}
fullDeck[i].setValue(val);
val++;
if(val > HIGH_VALUE) {
suitNum++;
val = 1;
}
}//end for
}
public void shuffle() {
for(int firstCard = 0; firstCard < DECK_OF_CARDS; firstCard++ ) {
firstCard = ((int)(Math.random() * 500) % DECK_OF_CARDS);
int secondCard = ((int)(Math.random() * 500) % DECK_OF_CARDS);
Card temp = fullDeck[firstCard];
fullDeck[firstCard] = fullDeck[secondCard];
fullDeck[secondCard] = temp;
}
}
}
BlackJack Class
public class BlackJack extends CardGameFP{
public BlackJack() {
**super();**
player1 = 2;
player2 = 2;
}
public void display() {
System.out.println("BlackJack");
}
public void dealCards() {
//Player 1
System.out.println("Player 1:");
for(int x = 0; x < player1; x++) {
System.out.println(fullDeck[x].getValue() + " of " + fullDeck[x].getSuit() );
shuffle();
}
//Player 2
System.out.println("\nPlayer 2:");
for(int x = 0; x < player2; x++) {
System.out.println(fullDeck[x].getValue() + " of " + fullDeck[x].getSuit() );
shuffle();
}
}
}
PlayerCardGame
public class PlayCardGame {
public static void main(String[] args) {
Card CG = new Card();
BlackJack BJ = new BlackJack();
BJ.display();
BJ.dealCards();
}
}
答案 0 :(得分:3)
好吧,你声明并初始化了rank
字符串(似乎编码得很好),但是你正在打印value
。
在二十一点课中,你有:
System.out.println(fullDeck[x].getValue() + " of " + fullDeck[x].getSuit() );
当你应该这样:
System.out.println(fullDeck[x].getRank() + " of " + fullDeck[x].getSuit() );