二十一点程序,字符串值问题
BlackJack Program, string value issue
我正在创建 BlackJack 程序。我目前正在 "number checking" 流程中。因此,一旦发牌并且玩家要求 "Hit" 我需要检查他们的牌是否超过 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()));
}
}
我会很感激 suggestions/advice。
你可以这样做:
import java.util.Map;
import java.util.HashMap;
Map<Int, String> dictionary = new HashMap<Int, String>();
然后添加每一项:
dictionary.put(1, "Ace");
正如 JacobIRR 在他的评论中所说,您可以使用 Map
,但我建议您将密钥用作 String
(卡片名称),并将值用作 Integer
(卡的价值)。
记住你不能给 Map
int
作为 Key,它必须是 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!");
我正在创建 BlackJack 程序。我目前正在 "number checking" 流程中。因此,一旦发牌并且玩家要求 "Hit" 我需要检查他们的牌是否超过 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()));
}
}
我会很感激 suggestions/advice。
你可以这样做:
import java.util.Map;
import java.util.HashMap;
Map<Int, String> dictionary = new HashMap<Int, String>();
然后添加每一项:
dictionary.put(1, "Ace");
正如 JacobIRR 在他的评论中所说,您可以使用 Map
,但我建议您将密钥用作 String
(卡片名称),并将值用作 Integer
(卡的价值)。
记住你不能给 Map
int
作为 Key,它必须是 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!");