第一次在这里张贴。我听说过很多关于社区的好事。我是一名业余程序员,到目前为止已涉足C ++和Java。 Anywhoo,我一直在用Java中的BlackJack游戏打开我的头,过去几天我有点随意地放在一起。基本上,我在制作我的套牌时遇到了问题。我使用ArrayList来保存卡片,但我无法找到一种方法来正确地为每张卡片添加一个值。正如你可以通过我的代码看到的那样,我使用parseint从String中获取值并将其添加到卡片总和但问题是,当“Ace”和“Jack”“King”“Queen”等值出现时,显然没有int可以从那里解析,所以当程序运行时,一切正常,除非当然提到前面提到的卡之一。然后我收到一条错误:
(“”线程中的异常“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.Blackjack.main(Blackjack.java:27) Java结果:1 建立成功(总时间:0秒)“”)。
我尝试在线查看是否可以激发一些灵感,但我能找到的只是使用Enums或Switch来构建我的套牌,但问题是我还没有完全理解这些技巧。任何建议将不胜感激。
谢谢!
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;
}
答案 0 :(得分:1)
支持密切注意此次通话。
totalValue2 += Integer.parseInt(value.cardValue);
它非常无害,因为它适用于cardValue
中的任何整数值。但是,你不仅要有积分价值 - 你也有文字。
您无法将“Queen”改为数字。 Java没有这种能力,因为它没有数字含义*。
最好将卡的价值和卡的名称存储为数据的单独部分,因为卡的名称还包括套装信息。
您还没有向我们展示您的Card类实现,但是如果不更改外部合同,您可以在获得String
时在内部进行一些解析,并返回{{1检索int
时,而不是String
。对于“Ace”类型,则根据手的总值,您应该选择最合适的值(1或11)。这部分我作为练习留给读者。
cardValue
*:“Queen”确实在基数31:24919187中有一个值。但这可能不是你想要的。