Мини-игра блэкджек - PullRequest
       6

Мини-игра блэкджек

0 голосов
/ 02 февраля 2020

Для 2 игроков в консольном приложении игра др aws цифры от 1 до 10 вместо карт. С помощью do-while l oop задается вопрос, хотите ли вы выбрать карту. У меня проблема с тем, чтобы дать правильное слово после ответа not, , потому что тогда l oop должен быть разбит, а когда он дает перерыв, он спрашивает еще и как вернуть его, выходит из программы в конце Программа говорит, кто победил.

`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);

1 Ответ

0 голосов
/ 02 февраля 2020

Вот версия, которая, кажется, делает то, что вам нужно с вашим текущим кодом. Я добавляю логическое условие (bool continuePlaying), чтобы оставаться внутри «do l oop» или нет.

    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();
        }
    }
}

Пожалуйста, примите ответ, если он вам поможет. Продолжайте кодировать:)

...