BlackJack Java卡片价值
第一次在这里发布。我听到了很多关于社区的好消息。我是一个业余程序员,到目前为止已经涉足C++和Java。无论如何,我一直在用java的这款BlackJack游戏来打开我的头脑,在过去的几天里,我几乎是随意地拼凑起来的。基本上,我在创建我的套牌时遇到了问题。我正在使用ArrayList来保存卡片,但我无法找到一种方法来正确地为每张卡片添加一个值。正如你可以看到我的代码,我使用parseint从字符串中获取值并将其添加到卡片总和中,但问题是,当诸如“Ace”和“Jack”“King”“Queen”等值出现时,显然没有int可以从那里解析,所以当程序运行时,一切正常,除非当然之前提到的卡之一被拉。然后我得到一个错误,内容如下:BlackJack Java卡片价值
(“线程中的异常”main“java.lang.NumberFormatException:对于输入字符串:”Queen“ at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 在java.lang.Integer.parseInt(Integer.java:580) 在java.lang.Integer.parseInt(Integer.java:615) 在blackjack.Blackjack.getHumanValue(Blackjack.java:110) 在二十一点。 Blackjack.main(Blackjack.java:27) Java结果:1 BUILD SUCCESSFUL(总时间:0秒)“”)。
我试过在网上查找它,看看我是否可以引发一些灵感,但我可以找到的是使用枚举或开关来构建我的套牌,但问题是我还没有完全理解这些技术。任何意见将不胜感激。
谢谢!
public class Blackjack{
ArrayList<card> buildDeck = new ArrayList<card>();
public static void main(String[] args)
{
Blackjack b = new Blackjack();
// Compares both scores. The largest score wins.
if(b.getHumanValue() < b.getRobotValue())
{
System.out.println("ROBOT WINS!!!");
}
else if(b.getRobotValue() > 21 && b.getHumanValue() <=21)
{
System.out.println("YOU WIN!!!");
}
}
public ArrayList<card> buildDeck(ArrayList<card> buildDeck)
{
String [] suit = {"Spade", "Hearts", "Clubs", "Diamonds"};
String [] cardValue = {"Ace","2","3","4","5","6",
"7","8","9","10","Jack","Queen","King"};
for(int i = 0; i < suit.length; i++)
{
for(int j = 0; j < cardValue.length; j++)
{
card card = new card(suit[i], cardValue[j]);
buildDeck.add(card);
}
}
return buildDeck;
}
// This returns the human score
public int getHumanValue()
{
buildDeck(buildDeck);
Scanner s = new Scanner(System.in);
card value;
int ranCard = 0;
int i = 0;
int aceValue = 0;
int totalValue = 0;
String drawAgain = "";
System.out.println("[[Dealer gives you two cards.]]");
System.out.println("");
while(i < 2)
{
ranCard = 0+(int)(Math.random()*52);
System.out.println("Card " + (i+1) + ":");
System.out.println(buildDeck.get(ranCard));
value = buildDeck.get(ranCard);
while(value.cardValue.equalsIgnoreCase("Ace")) // Ace value; either 1 or 11
{
System.out.println("");
System.out.println("This is an ace.");
System.out.println("It is equal to 1 or 11");
System.out.println("Which number do you prefer?");
System.out.println("");
aceValue = s.nextInt();
s.nextLine();
if(aceValue == 11)
{
totalValue += 11;
totalValue -= 1; // Subtracted 1; was off by one
break;
}
else if(aceValue == 1)
{
totalValue += 1;
totalValue -= 1; // Subtracted 1; was off by one
break;
}
}
totalValue += Integer.parseInt(value.cardValue);
System.out.println("Total Score: " + totalValue);
System.out.println("");
i++;
}
System.out.println("[[Would you like to draw another card? Yes/No]]");
System.out.println("");
drawAgain = s.nextLine();
System.out.println("");
if(drawAgain.equalsIgnoreCase("YES"))
{
ranCard = 0+(int)(Math.random()*52);
System.out.println("Card 3:");
System.out.println(buildDeck.get(ranCard));
value = buildDeck.get(ranCard);
totalValue += Integer.parseInt(value.cardValue);
System.out.println("Total Score: " + totalValue);
System.out.println("");
}
else
{
}
return totalValue;
}
// This returns the robot's score
// Mostly copied from gethumanValue method but more automated
public int getRobotValue()
{
buildDeck(buildDeck);
card value;
int ranCard = 0;
int i = 0;
int totalValue2 = 0;
System.out.println("[[Dealer gives Robot two cards.]]");
System.out.println("");
while(i < 2)
{
ranCard = 0+(int)(Math.random()*52);
System.out.println("Card " + (i+1) + ":");
System.out.println(buildDeck.get(ranCard));
value = buildDeck.get(ranCard);
while(value.cardValue.equalsIgnoreCase("Ace")) // Ace value; either 1 or 11
{
if(totalValue2 < 11)
{
totalValue2 += 11;
totalValue2 -= 1; // Subtracted 1; was off by one
break;
}
else if(totalValue2 > 10)
{
totalValue2 += 1;
totalValue2 -= 1; // Subtracted 1; was off by one
break;
}
}
totalValue2 += Integer.parseInt(value.cardValue);
System.out.println("Total Score: " + totalValue2);
System.out.println("");
i++;
}
if(totalValue2 < 17)
{
ranCard = 0+(int)(Math.random()*52);
System.out.println("Card 3:");
System.out.println(buildDeck.get(ranCard));
value = buildDeck.get(ranCard);
while(value.cardValue.equalsIgnoreCase("Ace")) // Ace value; either 1 or 11
{
if(totalValue2 < 11)
{
totalValue2 += 11;
totalValue2 -= 1; // Subtracted 1; was off by one
break;
}
else if(totalValue2 > 10)
{
totalValue2 += 1;
totalValue2 -= 1; // Subtracted 1; was off by one
break;
}
}
totalValue2 += Integer.parseInt(value.cardValue);
System.out.println("Total Score: " + totalValue2);
System.out.println("");
}
else
{
}
return totalValue2;
}
}
编辑对不起,忘了,包括我的卡类!
public class card {
String suit;
String cardValue;
@Override
public String toString() {
return cardValue + " of " + suit;
}
public card(String suit, String cardValue) {
this.suit = suit;
this.cardValue = cardValue;
}
public card(String cardValue) {
this.cardValue = cardValue;
}
public String getSuit() {
return suit;
}
public void setSuit(String suit) {
this.suit = suit;
}
收费密切关注这一号召。
totalValue2 += Integer.parseInt(value.cardValue);
它是无害的,因为它适用于cardValue
中的任何整数值。但是,你不仅有整体价值 - 你也有话语。
您不能将单词“女王”更改为数字。 Java没有这种能力,因为它没有数字意义*。
将卡的名称和卡的名称作为数据的单独部分存储会更好,因为卡的名称也会包含套装信息。
你还没有告诉我们您的卡类实现,但你可以在不改变你的外部合同做的是做了一下分析在里面,当你拿到String
,并返回int
,而不是一个String
时您检索cardValue
。在“Ace”类型的情况下,根据手的总价值,您应该选择最合适的值(1或11)。这部分我作为练习给读者。
public Card(String suit, String value) {
this.suit = suit;
switch(value) {
case "Ace":
isAce = true; // conditional logic req'd since Ace can be 1 or 11.
break;
case "Jack":
case "Queen":
case "King":
this.value = 10;
break;
}
this.cardName = value;
}
*:“皇后” 不在基座31的值:24919187.但是,这可能不是你所追求的。