Java getter 和 setter 卡片程序有问题

Java getter and setter trouble in card program

我正在为学校做作业,我遇到了一个我一生都无法解决的问题。

这只是一个简单的 "Card Game" 作业,我们基本上只是创建一副纸牌,洗牌并分发几张纸牌。

我的问题是 "dealing" Ace、Jack、Queen 和 King 仍然作为整数从 11-13 打印出来。我认为问题应该出在 setValue() 下的 Card class 中,但它似乎一切正常。我会先 post 那个 class 然后 post 剩下的以防万一你也需要查看它。

感谢您的帮助!!

P.S。我试图在书中查找但找不到答案...BlackJack class 是本书前一章的部分源代码,我不知道它是否是必须有 super();在构造函数那里?我认为只有当父 class 构造函数有参数时才需要这样做?关于它是否必须存在以及它在做什么的任何帮助。

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);
    }           
}   
}

卡牌游戏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;
    }
}
}

二十一点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();
    }
    }

好吧,您声明并初始化了 rank 字符串(似乎编码得很好),但您正在打印 value

在 blackjack class 中,您有:

System.out.println(fullDeck[x].getValue() + " of " + fullDeck[x].getSuit() );

什么时候你应该拥有这个:

System.out.println(fullDeck[x].getRank() + " of " + fullDeck[x].getSuit() );