我正在设计一个二十一点计划。我的问题是,我使用开关盒来生成随机卡。但是当需要比较卡片的价值时......可以说它if(pCard1 + pCard2 > 21)
不允许我比较它们,因为它们是字符串。但是,它们必须是字符串,因为我必须能够分配String "Queen"
,但我还需要“Queen”的 int 值为 10。我不知道该怎么做。
这是我到目前为止所拥有的。我没有包括我的整个代码,因为它有几百行长。仅包括二十一点部分。
System.out.println("---------------------Black Jack--------------------------");
System.out.println("Welcome to BlackJack!");
System.out.println("Available balance is $"+balance);
System.out.print("How much would you like to bet on this hand?: ");
int bet = input.nextInt();
balance -= bet;
System.out.println("You just bet $"+bet+"......Dealing cards!");
System.out.println("----------------------------------------------------------");
String pCard1 = dealCard();
String pCard2 = dealCard();
System.out.println("Your hand is a "+pCard1+" and "+pCard2);
System.out.print("Would you like to Hit or Stand? :");
String hOs = input.next();
if(hOs.equals("Hit")) {
String pCard3 = dealCard();
if(pCard1 + pCard2 + pCard3);
}
}
}
public static String dealCard() {
int value = (int)(Math.random() * 13);
String returnString = "";
switch ( value ) {
case 1: returnString = "Ace"; break;
case 2: returnString = "2"; break;
case 3: returnString = "3"; break;
case 4: returnString = "4"; break;
case 5: returnString = "5"; break;
case 6: returnString = "6"; break;
case 7: returnString = "7"; break;
case 8: returnString = "8"; break;
case 9: returnString = "9"; break;
case 10: returnString = "10"; break;
case 11: returnString = "Jack"; break;
case 12: returnString = "Queen"; break;
case 13: returnString = "King"; break;
}
return returnString;
}
}