java - 使用骰子的二十一点游戏

标签 java dice blackjack

我正在用 Java 做一个项目,其中我使用骰子来模拟二十一点游戏,但是,我遇到了一个我写了错误代码的地方。如果玩家得分高于他,我试图让经销商模拟行动。另外,我的 checkWinner() 方法似乎在错误的时间激活。我不是要求任何人修复它,而是告诉我代码哪里有问题。

public class BlackJack {

    static PairOfDice cards = new PairOfDice();
    static Scanner scan = new Scanner(System. in );
    static int playerScore, dealerScore, player, dealer, tempScore;
    static boolean newGame;
    static String play, hit;


    public static void main(String[] args) {
        newGame();
        dealerRoll();
        playerRoll();

        // checkWinner();
    }

    public static void newGame() {

        System.out.println("Would you like to play blackjack? y or n?");
        play = scan.nextLine();

        if (play == "y"); {
            newGame = true;
        }
        if (play != "y") newGame = false;
    }

    public static void dealerRoll() {
        while (newGame) {
            cards.rollDice();
        }

        dealerScore = (cards.rollDice());
        System.out.println("Dealer score is: " + dealerScore);

        if (dealerScore <= 15 && playerScore > dealerScore) tempScore = cards.rollDice();
        System.out.println("The dice roll was: " + tempScore);
        dealerScore += tempScore;
        System.out.println("Dealer score is: " + dealerScore);

    }

    public static void playerRoll() {
        while (newGame) {
            cards.rollDice();
        }

        playerScore = (cards.rollDice());

        System.out.println("You total is " + playerScore);

        while (playerScore < 21 || playerScore < dealerScore) {

            System.out.println("Would you like to take a hit? y or n?");
            hit = scan.nextLine();
            if (hit.equals("y")) {
                tempScore = cards.rollDice();
                System.out.println("The dice roll was: " + tempScore);
                playerScore += tempScore;
                System.out.println("Your score is now: " + playerScore);

            }
            else if (hit.equals("n")); {
                checkWinner();
            }
        }
    }

    public static void checkWinner() {
        if (playerScore == 21) System.out.println("You win!");
        else if (dealerScore == 21) System.out.println("Sorry, Dealer has won!");
        else if (dealerScore > playerScore) System.out.println("Sorry, Dealer has won!");
        else if (playerScore > dealer) System.out.println("You win!");
        else if (playerScore > 21) System.out.println("Sorry you busted, Dealer has won!");

        else if (dealerScore > 21) System.out.println("Dealer has busted, You win!");
    }
}​

最佳答案

完成玩家回合后,您无法检查获胜者。只有在经销商转弯后才做。

关于java - 使用骰子的二十一点游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13212161/

相关文章:

java - 我应该在编辑 Java 文件的同时只修改 Android 的 XML 吗?

java - 将装饰器模式应用于 Java String 类

python - (Python) while 循环打破了我的 random_number

ruby - 二十一点游戏的数据结构

java - 为二十一点程序返回 bufferdimage 的方法

android - 从 raw 播放音频时 avd 崩溃

java - 与 Statement 相比,使用 PreparedStatement 有什么缺点吗

java - Solrj 与 Solr Suggester

python - 掷骰子程序的无限 while 循环问题

java - 掷骰子程序,但我需要从不同的类传输输入数据