为什么我的老师告诉我,当我的代码运行正常时,我的return语句是错误的

时间:2019-04-01 01:17:43

标签: java computer-science

我正在制作一个简单的Black Jack游戏,然后完成并以为我问我的老师他的想法,因为该代码有效,但是他说我的return语句不正确。我知道它们处于空白状态,但是我并不想返回任何东西,而只是想在输赢时返回到main方法(即主菜单)。为什么这不正确,或者我怎么告诉他这是正确的。

我尝试将main方法放入其他方法中,但是没有用,除了我对Java相当陌生(通常是编码)并且需要一些帮助。

       {
        System.out.println("\n READY AND START");
        Scanner input = new Scanner(System.in);
        System.out.println("What is your name?");
        String name = input.nextLine();
        System.out.println("what is the dealer called?");
        String cname = input.nextLine();
        System.out.println("How much money are you willing to bet?");
        int wager = input.nextInt();
        int playerTotal, computerTotal, playerRunning, computerRunning;
        playerTotal = 0;
        computerTotal = 0;
        Random cards = new Random();
        for (int i = 1; i <= 2; i++) {
            playerRunning = 2 + cards.nextInt(10);
            playerTotal += playerRunning;
            System.out.println(name + " your starting card is " + playerRunning);
        }
        System.out.println(name + " your starting hand total is " + playerTotal);
        if (playerTotal == 21) {
            System.out.println("i guess the house doesnt always win" + name + " you got a blackjack here is your money  $" + (wager * 1.5));
            return;
        } else if (playerTotal > 21) {
            System.out.println(name + " you are bust LOL, thank you for the $" + wager);
            return;
        }
        computerRunning = 2 + cards.nextInt(10);
        computerTotal += computerRunning;
        System.out.println(cname + " the dealers starting card is " + computerRunning);
        computerRunning = 2 + cards.nextInt(10);
        computerTotal += computerRunning;
        System.out.println(cname + " the dealers is keeping their second card hidden");
        if (computerTotal == 21) {
            System.out.println(computerTotal + " the house always wins haha, you lost your wager of $" + wager);
            return;

        } else if (computerTotal > 21) {
            System.out.println(computerTotal + "The house has busted with here is your money $" + (wager * 2));
            return;
        }
        input = new Scanner(System.in);
        System.out.println(name + " do you want to continue? The dealer thinks you can continue. \n Y or N");
        String hit = input.nextLine();
        while (hit.equalsIgnoreCase("y")) {
            playerRunning = 2 + cards.nextInt(10);
            playerTotal += playerRunning;
            System.out.println(name + " your new card is " + playerRunning);
            System.out.println(name + " your hand total is " + playerTotal);
            if (playerTotal == 21) {
                System.out.println("  \ni guess the house doesnt always win" + name + " well here is your money and sum $" + (wager * 2));
                return;
            } else if (playerTotal > 21) {
                System.out.println(name + " \nyou are bust LOL, thank you for the wager $" + wager);
                return;
            }
            if (computerTotal > 16) {
                System.out.println(cname + " the dealers is okay with their hand");
            } else {
                computerRunning = 2 + cards.nextInt(10);
                computerTotal += computerRunning;
                System.out.println(cname + " the dealers draws another card");
                if (computerTotal == 21) {
                    System.out.println(cname + " the dealer got " + computerTotal + " the house always wins haha, you lost your wager of $" + wager);
                    return;
                } else if (computerTotal > 21) {
                    System.out.println(computerTotal + " The house has busted, you got doulbe your wager $" + (wager * 2));
                    return;
                }
            }
            System.out.println(name + " do you want a hit? The dealer thinks you can continue. \n Y or N");
            hit = input.nextLine();
        }
        do {
            computerRunning = 2 + cards.nextInt(10);
            computerTotal += computerRunning;
            System.out.println(cname + " the dealers draws another card");
            if (computerTotal == 21) {
                System.out.println(cname + " the dealer got " + computerTotal + " the house always wins haha, you lost your wager of $" + wager);
                return;
            } else if (computerTotal > 21) {
                System.out.println(computerTotal + " The house has busted, you got doulbe your wager $" + (wager * 2));
                return;
            }
        } while (computerTotal < 16);
        if (playerTotal > computerTotal && playerTotal <= 21) {
            System.out.println(name + " i guess the house doesnt always win, here is your money $" + (wager * 2));
        } else if (computerTotal > playerTotal && computerTotal <= 21) {
            System.out.println("the house always wins haha, the dealer had " + computerTotal + " thank you for the money $" + wager);
        } else if (computerTotal == playerTotal && computerTotal <= 21 && playerTotal <= 21) {
            System.out.println("WE DRAWED??? sigh good  game i guess BUT house always wins so thank you for the $" + wager);
        } else if (computerTotal > 21 && playerTotal > 21) {
            System.out.println("and we both busted so no one wins");
        }
    }
;
``````

这只是我的游戏方法的一部分,具有我的返回功能,并且您可以看到它是一个带有返回的空格,因为我不想返回任何内容,我只想返回主菜单而不显示任何内容。真正节省。游戏运行正常,我不知道为什么会出现问题

1 个答案:

答案 0 :(得分:0)

如上文markspace所述,对21点的这种解释的整体流程是有缺陷的。

但是,您的退货说明不正确的主要原因可能是,如果玩家和发牌人都收到二十一点(21),则说明是推送(钱移至下一轮)或您收回了钱。获得二十一点时,您永远都不应亏钱。

查看以下链接以查看游戏流程:https://www.practice-blackjack.com/blackjack-the-flow-of.html

另一个原因可能是您需要下注并将结果返回到函数中,以加回到玩家拥有的总金额中。