1

Я написал простенькую игру, в которой угадываешь числа для двоих игроков. Но не могу понять, как выводить сообщение для другого игрока, ведь цикл не проверяет его значения. Объясните, пожалуйста, как это исполнить и что я делаю не так.


namespace hw2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Game");

            Random rand = new Random();

            int bone1 = rand.Next(1, 101);
            int bone2 = rand.Next(1, 101);
            int count = 0;
            int player1;
            int player2;

            Console.Write("Enter name: ");
            string p1 = Console.ReadLine();
            Console.Write("Enter name: ");
            string p2 = Console.ReadLine();

            for (; ; )
            {
                Console.Write($"\n{p1} enter number: ");
                count++;
                player1 = Convert.ToInt32(Console.ReadLine());
                Console.Write($"\n{p2} enter number: ");
                count++;
                player2 = Convert.ToInt32(Console.ReadLine());

                if (player1 < bone1 || player1 > bone1)
                {
                    if (player1 < bone1)
                {
                    Console.WriteLine($"\n{p1} entered number is less than the guess. Try again!");
                }
                else if (player1 > bone1)
                {
                    Console.WriteLine($"\n{p1} entered number is greater than the envisioned one. Try again!");
                }
            }
            else if (player2 < bone2 || player2 > bone2)
            {
                if (player2 < bone2)
                {
                    Console.WriteLine($"\n{p2} entered number is less than the guess. Try again!");
                }
                else if (player2 > bone2)
                {
                    Console.WriteLine($"\n{p2} entered number is greater than the envisioned one. try again!");
                }
            }

                else
                {
                    if (player1 == bone1)
                    {
                        Console.WriteLine($"\n{p1} congratulations, the number is right! Attempts: {count}.");
                        Console.ReadKey();
                        break;
                    }

                    else if (player2 == bone2)
                    {
                        Console.WriteLine($"\n{p2} congratulatipns, the number is right! Attempts: {count}.");
                        Console.ReadKey();
                        break;
                    }


                }
            }
        }
    }
}
0

1 ответ 1

0

Уберите из Вашего кода все else.

7
  • Вы имеете ввиду else if и else?
    – RinSama
    10 июн 2021 в 14:20
  • @RinSama Нет, я имею в виду только слово else.
    – user176262
    10 июн 2021 в 14:22
  • А как мне тогда цикл закончить?
    – RinSama
    10 июн 2021 в 14:23
  • @RinSama Так же как и сейчас.
    – user176262
    10 июн 2021 в 14:24
  • Спасибо это действительно заработало, но почему?
    – RinSama
    10 июн 2021 в 14:26

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.