【问题标题】:BlackJack Program, string value issue二十一点计划,字符串值问题
【发布时间】:2018-09-10 11:57:20
【问题描述】:

我正在创建一个 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()));
  }
}

如果有任何建议/建议,我将不胜感激。

【问题讨论】:

  • 不是字符串数组,而是一个字典/哈希表,其中键是 int,值是字符串?
  • 你能解释一下吗?没听说过。
  • 你可以这样做:Map&lt;Int, String&gt; dictionary = new HashMap&lt;Int, String&gt;(); 然后添加每个项目:dictionary.put(1, "Ace");
  • 好的,实现了这个,但它告诉我 Map 和 HashMap 不能解析为一个类型。必须导入一些东西才能使用它吗?
  • import java.util.Map;import java.util.HashMap;

标签: java blackjack gambling


【解决方案1】:

你可以这样做:

import java.util.Map;
import java.util.HashMap;
Map<Int, String> dictionary = new HashMap<Int, String>();

然后添加每个项目:

dictionary.put(1, "Ace");

【讨论】:

    【解决方案2】:

    正如 JacobIRR 在他的评论中所说,您可以使用 Map,但我建议您使用密钥作为 String(卡名)和值作为 Integer(卡的值)。

    记住你不能给Mapint作为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!"); 
    

    【讨论】:

    • 我这样做了,但它似乎与我的随机抓卡方法混淆了。字符串 pCard1 = cards.get(cards.nextInt(cards.size()));不再有效,因为它现在是一个字符串。
    • 你可以查看这个答案:stackoverflow.com/a/12385392/4780434 获取所有keyset的Array,并将随机抓卡方法放入其中。
    猜你喜欢
    • 2012-01-03
    • 1970-01-01
    • 2011-02-02
    • 1970-01-01
    • 2012-03-15
    • 2016-06-21
    • 2013-06-07
    • 2016-12-29
    • 2011-01-25
    相关资源
    最近更新 更多