我正在创建一个BlackJack程序。我目前正处于“号码检查”过程中。因此,一旦牌被发出并且牌手要求“击球”,我需要检查他们的牌是否超过,21。 我试过这个:
if(pCard1+pCard2+pCard3 > 21)
System.out.println("Bust!");
但很快我意识到因为我的卡阵列是一个字符串数组,我不能使用数学运算符来检查卡是否超过21.我想知道是否还有为每个分配一个Int值我的字符串,以便我可以在数学上检查它们是否超过21。
else if(game == 3) {
int bet = 0;
String HoS;
ArrayList<String> cards = new ArrayList<String>();
cards.add("1");
cards.add("2");
cards.add("3");
cards.add("4");
cards.add("5");
cards.add("6");
cards.add("7");
cards.add("8");
cards.add("9");
cards.add("10");
cards.add("Jack");
cards.add("Queen");
cards.add("King");
cards.add("Ace");
System.out.println("------------------BLACKJACK---------------------");
System.out.println("Welcome to BlackJack!");
System.out.println("Current balance $"+balance);
System.out.print("How much would you like to bet on this hand?:");
bet = input.nextInt();
System.out.println("--------------------------------------------------------------------");
System.out.println("Dealing cards.........");
Random card = new Random();
String pCard1 = cards.get(card.nextInt(cards.size()));
String pCard2 = cards.get(card.nextInt(cards.size()));
System.out.println("Your hand is a "+pCard1+","+pCard2);
System.out.print("Would you like hit or stand?:");
HoS = input.next();
if(HoS.equals("Hit")) {
String pCard3 = cards.get(card.nextInt(cards.size()));
System.out.print("*Dealer Hits* The card is "+pCard3);
}
else if(HoS.equals("Stand")) {
System.out.println("Dealing Dealer's hand........");
String dCard1 = cards.get(card.nextInt(cards.size()));
String dCard2 = cards.get(card.nextInt(cards.size()));
}
}
我很感激任何建议/意见。
答案 0 :(得分:1)
你可以这样做:
import java.util.Map;
import java.util.HashMap;
Map<Int, String> dictionary = new HashMap<Int, String>();
然后添加每个项目:
dictionary.put(1, "Ace");
答案 1 :(得分:0)
正如JacobIRR在评论中所说,您可以使用Map
,但我建议您将密钥用作String
(卡片名称),将值作为Integer
(值的卡)。
请注意,您无法将Map
int
作为密钥,它必须是Integer
(您不能在Map
中使用原始类型)
会是这样的:
Map<String, Integer> cards = new HashMap<String, Integer>();
并把你所有的卡片都放在这样:
cards.put("1", 1);
cards.put("2", 2);
...
cards.put("Jack", 11);
cards.put("Queen", 12);
cards.put("King", 13);
cards.put("Ace", 1);
然后你的if condition
会是这样的:
if(cards.get(pCard1) + cards.get(pCard2) +cards.get(pCard3) > 21)
System.out.println("Bust!");