代码之家  ›  专栏  ›  技术社区  ›  NoobCoderChick

卡程序中的Java getter和setter问题

  •  0
  • NoobCoderChick  · 技术社区  · 9 年前

    我正在为学校做作业,我有一个终生无法解决的问题。

    这只是一个简单的“纸牌游戏”任务,我们基本上只是创建一副牌,洗牌并分发几张牌。

    当“处理”Ace、Jack、Queen和King的卡时,我的问题就出现了。我认为问题应该出现在setValue()下的Card类中,但这一切似乎都没问题。我会先发布该类,然后发布剩余部分,以备您需要查看。

    谢谢你的帮助!!

    注:我试图在书中查找这个问题,但找不到答案……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类

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

    21点类

     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();
        }
        }
    
    1 回复  |  直到 9 年前
        1
  •  3
  •   Alexander Guyer    9 年前

    嗯,您声明并初始化了 rank 字符串(看起来编码很好),但您正在打印 value 相反

    在21点课程中,你有:

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

    当你应该这样做时:

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