Minigame 21点

问题描述 投票:-2回答:1

对于控制台应用程序中的2个玩家,游戏从1到10抽取数字,而不是纸牌。通过do-while循环询问问题,是否要选择卡。我在answer not,之后给出正确的单词时遇到问题,因为然后循环应该被中断,并且当它给出中断时,它仍会询问,如何返回该循环,该程序最终会提示谁赢了。

`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个回答
0
投票

这里是一个版本,该版本似乎可以满足您当前代码的需要。我添加了一个布尔条件(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();
        }
    }
}

如果可以帮助您,请接受答案。继续编码:)

© www.soinside.com 2019 - 2024. All rights reserved.