Java二十一点程序

时间:2015-07-30 00:46:27

标签: java methods numberformatexception

所以我用你的建议(Todd Hopp)重写了它,并更新了BlackJack课程中(*)部分的帖子,这样你就可以看到我现在如何拥有它,但我仍然得到了同样的结果错误,它不会出于某种原因打印computePlayerValue ....

错误:     线程" main"中的例外情况java.lang.NumberFormatException:对于输入字符串:" King" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)

at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at BlackJack.dealCards(BlackJack.java:25)
at PlayCardGame.main(PlayCardGame.java:9)

P.S。我试图在书中查一下但是找不到答案...... BlackJack类是本书前一章的部分源代码,我无法弄清楚是否有必要有super();在构造函数那里?我认为只有在父类构造函数有参数时才需要这样做?如果它必须在那里,任何帮助,如果是这样的话。

BlackJack Class

 public class BlackJack extends CardGameFP{
    int computePlayerValue;
    int computeDealerValue;
    int cardValue;

    public BlackJack() {
        **super();**
        player1 = 2;
        player2 = 2;
    }

    public void display() {
        System.out.println("BlackJack");
    }
//************************************************************* 
    public int getCardValue(Card card) {
    final String rank = card.getRank();
    switch (rank) {
        case "Ace":
            return 1;
        case "King":
        case "Queen":
        case "Jack":
            return 10;
        default:
            return Integer.parseInt(rank);
    }
}

public void dealCards() {
    //Player 1
    System.out.println("Player 1:");
    for(int x = 0; x < playerHand; x++) {
        shuffle();
        System.out.println(fullDeck[x].getRank() + " of " + fullDeck[x].getSuit());
    }
    cardValue1 = getCardValue(fullDeck[0]);
    cardValue2 = getCardValue(fullDeck[1]);
    computePlayerValue = cardValue1 + cardValue2;
    System.out.println(computePlayerValue);
}

//*************************************************************


        //Dealer hand
        System.out.println("\nPlayer 2:");
        for(int x = 0; x < player2; x++) {
            System.out.println(fullDeck[x].getRank() + " of " + fullDeck[x].getSuit() );
            shuffle();
        }
    }
    }


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

PlayCardGame Class

PlayerCardGame

    public class PlayCardGame {

    public static void main(String[] args) {
        Card CG = new Card();
        BlackJack BJ = new BlackJack();

        BJ.display();
        BJ.dealCards();
    }
    }

2 个答案:

答案 0 :(得分:2)

而不是这一行:

cardValue = Integer.parseInt(fullDeck[0].getRank());

我建议你创建一个getCardValue(Card)方法:

public int getCardValue(Card card) {
    final String rank = card.getRank();
    switch (rank) {
        case "Ace":
            return 1;
        case "King":
        case "Queen":
        case "Jack":
            return 10;
        default:
            return Integer.parseInt(rank);
    }
}

然后使用:

cardValue = getCardValue(fullDeck[0]);

编辑:根据@WorldSEnder的建议,您可以定义Rank枚举:

public enum Rank {
    ACE("Ace", 1),
    TWO("2", 2),
    ...
    QUEEN("Queen", 10),
    KING("King", 10);

    private final String displayName;
    private final int value;

    protected Rank(String displayName, int value) {
        this.displayName = displayName;
        this.value = value;
    }

    public String displayName() {
        return displayName;
    }

    public int value() {
        return value;
    }
}

然后,您可以修改Card以使用Rank代替String作为排名并使用:

cardValue = fullDeck[0].getRank().value();

答案 1 :(得分:1)

致电时:

 cardValue = Integer.parseInt(fullDeck[0].getRank());    

方法Interger.parseInt()正在尝试从int读取string。问题是某些卡片的排名本身不是King之类的字符串。因此,当您返回该卡的排名时,parseInt()不知道如何处理King。您应该使用fullDeck[0].getVaule()获取卡的实际值,然后解析它。它将是1到13之间的字符串。