【问题标题】:Black Jack Game in JavaJava中的二十一点游戏
【发布时间】:2014-08-29 10:09:23
【问题描述】:

问题陈述

我对此有另一个主题,但我似乎无法找到它。基本上,我有一个黑杰克游戏。给用户两张随机卡片(然后将这些卡片相加,并显示总数)。然后它会提示用户他们是否想要另一张卡(他们几乎希望总数低于 21)。如果他们选择“是”,他们将获得一个随机卡号(他们可以继续获得一张卡,但应避免超过 21),但如果他们选择“否”,则游戏停止。

这是我应该得到的输出:[blackjackoutput.jpg]

这就是我得到的:[output1.jpg]

源码:

public class BlackJackGame {
    public static void main(String[] args) {
        int randomnum1 = (int) (1 + Math.random() * 10);
        int randomnum2 = (int) (1 + Math.random() * 10);
        int total;
        char anotherCard = 'y';
        char playAgain;
        Scanner input = new Scanner(System.in);

        // Prints cards player starts off with
        System.out.println("First cards: " + randomnum1 + ", " + randomnum2);

        // Sum of the 2 cards
        total = randomnum1 + randomnum2;

        // Prints Total
        System.out.println("Total: " + total);

        // Do While Loop that asks question to get lower than 21 or terminate.
        while (anotherCard != 'n') {
            if (total <= 21) {
                System.out.print("Do you want another card? (y/n): ");
                anotherCard = input.next().charAt(0);

                int randomnum3 = (int) (1 + Math.random() * 10);
                System.out.println("Card: " + randomnum3);

                total += randomnum3;
                System.out.println("Total: " + total);
            } else if (total > 21) {
                System.out.println("BUST.");

                System.out.print("Would you like to play again? (y/n): ");
                playAgain = input.next().charAt(0);
            }
        } 
    }
}

问题

  1. 当我达到 21 时,我选择“否”来停止程序。但是,它会继续显示下一张卡片和更新后的总数。

  2. 当我“破产”时。 (或超过 21 个)。它会问我是否想玩。在不同的场合,我选择“y”,它说“Bust”。并问我是否想再玩一次(它循环播放,说“BUST。”,并问我同样的问题,但无法结束程序)。同样的事情,如果我选择“否”,它会说“胸围”。并问我是否可以再玩一次。

  3. 你又是怎么玩这个游戏的?

请帮忙!!!

【问题讨论】:

  • this 是您的其他问题吗? (此链接将显示您在 Stack Exchange 上的活动:stackoverflow.com/users/3385997
  • 这是多次尝试的更新。我想从任何人那里知道我做错了什么。
  • @ChiefTwoPencils:抱歉,我没有看到格式化程序转换了 HTML 实体...

标签: java blackjack


【解决方案1】:

您遇到的问题是这里的逻辑问题:

 if (total <= 21) 
    {
      System.out.print("Do you want another card? (y/n): "); //<--------------
      anotherCard = input.next().charAt(0);

      int randomnum3 = (int) (1 + Math.random() * 10);

      System.out.println("Card: " + randomnum3);

      total += randomnum3;

      System.out.println("Total: " + total);

您需要检查if(anotherCard == 'n') 才能跳出循环

旁注

这个二十一点游戏应该有更好的随机牌分布,更好地模拟 52 张牌组

【讨论】:

    猜你喜欢
    • 2013-10-08
    • 1970-01-01
    • 2023-03-21
    • 2020-05-18
    • 1970-01-01
    • 2021-05-29
    • 1970-01-01
    • 2014-10-22
    • 2022-11-12
    相关资源
    最近更新 更多