Как посчитать ответы пользователей в консоли - PullRequest
0 голосов
/ 13 марта 2020

Первые дни с C# и уже застряли. У меня вопрос, как посчитать все ответы, которые дал пользователь, пытаясь выяснить, что такое случайное число? А затем вставьте его в предложение Лас с правильным ответом. Вот как выглядит «викторина»:

public static void Main(string[] args)
    {
        Console.WriteLine("The secret number\n");

        Random randomerare = new Random();
        int slump_tal = randomerare.Next(1, 101);

        Console.WriteLine("Write your number and we'll see where it lands:\n");
            string user_nr = Console.ReadLine();
            int tal = Convert.ToInt32(user_nr);
            Console.WriteLine();

            while (tal > slump_tal)
        {
                Console.WriteLine("Wrong! Your number is too big!\n");
                user_nr = Console.ReadLine();
                tal = Convert.ToInt32(user_nr);
        }
            while (tal < slump_tal)

        {
            Console.WriteLine("Wrong Your number is too low!\n");
                user_nr = Console.ReadLine();
                tal = Convert.ToInt32(user_nr);
        }

            while (tal == slump_tal)
            {
        Console.WriteLine("Bravo! That's the correct number\n");
            break;
            }

        Console.WriteLine("The secret number was: {0}\n\nPush the button to finish", slump_tal);

        Console.ReadKey(true);
    }

Ответы [ 2 ]

2 голосов
/ 13 марта 2020

В вашем коде есть 2 шага. И вы их перепутали.

Шаг 1: сделайте некоторое время l oop, чтобы продолжать получать информацию от пользователя.

Шаг 2: внутри каждого l oop вам нужно проверить ввод против числа.

Это должен быть 1 большой while () l oop с 3 if (). Дайте мне знать, если вам нужен пример кода.

0 голосов
/ 13 марта 2020

Я бы сделал это так:

 1. Get a number
 2. While the user is not hitting the right number:
      2.1 let the user know if the number was too big or too small
      2.2 ask for another number
      2.3 count number-of-attems + 1
 3. If the user arrived here, it means it has the number right, print number-of-attems

В коде это может выглядеть так:

    public static void Main(string[] args)
    {
        Console.WriteLine("The secret number\n");

        Random randomerare = new Random();
        int slump_tal = randomerare.Next(1, 101);

        Console.WriteLine("Write your number and we'll see where it lands:\n");
        Console.WriteLine();


        int user_nr = Convert.ToInt32(Console.ReadLine());

        // Here you can store the attempts
        int attempts = 1;

        // While the user provides the wrong answer, we iterate
        while(slump_tal != user_nr)
        {

            // We add 1 to the counter
            attempts++;

            if (slump_tal > slump_tal)
            {
                Console.WriteLine("Wrong Your number is too low!\n");
            }
            else 
            {
                Console.WriteLine("Wrong! Your number is too big!\n");
            }

            user_nr = Convert.ToInt32(Console.ReadLine());
        }

        Console.WriteLine($"Bravo! That's the correct number. you did it in {attempts} attemps");
        Console.WriteLine("The secret number was: {0}\n\nPush the button to finish", slump_tal);
        Console.ReadKey(true);
    }
...