【问题标题】:Why is my teacher telling me, my return statements are wrong when my code works perfectly为什么我的老师告诉我,当我的代码完美运行时,我的返回语句是错误的
【发布时间】:2019-04-01 01:17:43
【问题描述】:

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

我尝试将我的 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");
        }
    }
;
``````

这只是我的游戏方法的一部分,它有我的返回功能,你可以看到它是一个带返回的 void,因为我不想返回任何东西我只想回到主菜单没有真的省。游戏可以运行我不知道为什么会出现问题

【问题讨论】:

  • 好吧,也许是因为您首先完成了整个计算机的操作,而没有播放器(我可以看到)。二十一点不是这样玩的。
  • 我添加了整个代码,所以你现在可以看到

标签: java computer-science


【解决方案1】:

正如上面 markspace 所评论的,这种对二十一点的解释的整体流程是有缺陷的。

但是,您的退货声明不正确的主要原因可能是,如果玩家和庄家都收到二十一点 (21),那么它要么是推动(资金转移到下一轮),要么您收到了退款。获得二十一点时,您永远不应该赔钱。

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

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

【讨论】:

  • 我更新了,所以你可以看到整个事情,我不想在有人获胜后立即返回任何东西,我希望它进入主菜单并且没有任何返回,以便他们可以如果他们愿意,可以开始一个全新的游戏。
  • 你有函数名还是这都是程序化的?同样如我的回答中所述,您的老师仍然有可能指的是不正确的游戏流程。例如。平局会导致玩家在本应保留资金的地方赔钱。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-18
  • 1970-01-01
  • 2021-04-16
  • 2020-07-23
  • 1970-01-01
  • 2020-02-16
相关资源
最近更新 更多