简体   繁体   English

Java 21点程序

[英]Java blackjack program

So I rewrote it with your suggestion (Todd Hopp) and updated the post in the (*) section in the BlackJack class so you can see how I have it now but I'm still getting the same errors and it is not printing the computePlayerValue for some reason.... 所以我用您的建议(Todd Hopp)重写了它,并更新了BlackJack类的(*)部分中的帖子,这样您可以看到我现在的情况,但是我仍然遇到相同的错误,并且它没有打印computePlayerValue由于某些原因....


ERROR: Exception in thread "main" java.lang.NumberFormatException: For input string: "King" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 错误:线程“ main”中的异常java.lang.NumberFormatException:对于输入字符串:java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)的“ King”

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)

PS I'm was trying to look this up in the book but couldn't find the answer...The BlackJack class is partially source code from an earlier chapter in the book and I couldn't figure out if it was necessary to have super(); PS我正在尝试在书中查找这个问题,但找不到答案... BlackJack类是本书前一章的部分源代码,我无法弄清楚是否有必要超(); in the constructor there? 在构造函数那里? I thought it only had to do if the parent class constructor had an argument? 我以为只有父类构造函数有参数时才要做? Any help on if it has to be there and if so what it's doing. 关于是否必须存在以及它在做什么的任何帮助。

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 PlayCardGame类

PlayerCardGame

    public class PlayCardGame {

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

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

Instead of this line: 代替此行:

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

I suggest that you create a getCardValue(Card) method: 我建议您创建一个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);
    }
}

and then use: 然后使用:

cardValue = getCardValue(fullDeck[0]);

EDIT: Following the suggestion by @WorldSEnder, you can define a Rank enum: 编辑:根据@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;
    }
}

Then you can modify Card to use a Rank instead of a String for the rank and use: 然后,您可以修改Card以使用Rank而不是String进行排名并使用:

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

When you call : 你打电话时 :

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

the method Interger.parseInt() is attempting to read an int from a string . 方法Interger.parseInt()尝试从string读取int The problem is that some cards have ranks which themselves are not strings like, King . 问题在于某些卡的行列本身不是字符串,例如King So when you return the rank of that card, parseInt() doesn't know how to handle King . 因此,当您返回该卡的等级时, parseInt()不知道如何处理King You should instead be getting the cards actual value using fullDeck[0].getVaule() and parsing that instead. 相反,您应该使用fullDeck[0].getVaule()获取卡的实际值,然后解析该值。 It will be a string between 1 and 13. 这是介于1到13之间的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM