【问题标题】:Minigame blackjack小游戏二十一点
【发布时间】:2020-05-18 11:15:57
【问题描述】:

对于控制台应用程序中的 2 名玩家,游戏会抽取 1 到 10 的数字而不是卡片。使用 do-while 循环询问您是否要选择一张卡片。我在 answer not, 之后给出正确的词时遇到问题,因为这样循环应该被打破,当它给出 break 时它仍然询问以及如何返回它在程序说的最后退出程序谁赢了。

`enter code here` Console.WriteLine("now the first player's turn");
        int number = 0;
        Random r = new Random();

        ` do
        {
            Console.WriteLine("Are you downloading the card?");
            string odp = Console.ReadLine();
            switch (odp)
            {
                case "yes":
                    int rInt = r.Next(1, 10);
                    number += rInt;
                    Console.WriteLine(number);
                    break;
                case "not":
                    ?

            }
            if (number >= 22)
            {
                Console.WriteLine("The player 1 lost with {0} pkt", number);
                break;
            }


        } while (number < 22);

【问题讨论】:

  • 您的问题到底是什么,到目前为止您尝试过什么?你能分享一些代码吗?
  • 请使用代码编辑您的问题,而不是将其作为评论发布。

标签: c# blackjack


【解决方案1】:

这是一个似乎可以满足您当前代码所需的版本。 我添加了一个布尔条件(bool continuePlaying)来保持是否在“do loop”内。

    using System;

    namespace BlackJack
    {
        class Program
        {
            static void Main(string[] args)
            {
            Console.WriteLine("First player's turn");

            int number = 0;
            Random random = new Random();
            bool continuePlaying = true;

            do
            {
                Console.WriteLine("Are you downloading the card? [Y]es/[N]o");
                string userAnswer = Console.ReadLine();

                switch (userAnswer.ToLower())
                {
                    case "y":
                        int randomNumber = random.Next(1, 10);
                        number += randomNumber;

                        Console.WriteLine($"Your total of points is: {number}");
                        continuePlaying = true;
                        break;

                    case "n":
                        Console.WriteLine($"Your total of points is: {number}");
                        continuePlaying = false; // Stop playing
                        break;

                    default:
                        Console.Clear();
                        Console.WriteLine("Please choose [Y]es or [N]o");
                        continuePlaying = true;
                        break;
                }
            } while (number < 22 && continuePlaying == true);

            if (number <= 21)
            {
                Console.WriteLine($"You end the game with a total of {number} points");
            }
            else
            {
                Console.WriteLine($"The player 1 lost with {number} points");
            }
            Console.ReadLine();
        }
    }
}

如果对您有帮助,请采纳答案。 继续编码:)

【讨论】:

  • 非常感谢,您帮了我的忙:)
猜你喜欢
  • 2023-03-21
  • 1970-01-01
  • 2014-10-22
  • 2022-11-12
  • 2012-10-16
  • 2015-06-02
  • 1970-01-01
  • 2012-07-27
  • 2013-10-08
相关资源
最近更新 更多