简体   繁体   中英

C# console app random number generation

How can I get the random number generator method to loop and produce new random numbers? Do I need a separate class before Main that handles the random number generation? If so, how do I get the scope of those variables to Main? The console app is supposed to repeat integer math problems to practice with a loop that conditions on correct or incorrect answer. Also, what syntax am I overlooking to loop back to a new instance of randomly generated integers? TIA.

    public class Multiplication
    {
        public static void Main(string[] args)
        {
            Random randomNumbers = new Random(); // random number generator
            int num01 = randomNumbers.Next(1, 11);
            int num02 = randomNumbers.Next(1, 11);
            int value = 0;
            while (true)
            {
                if (value != -1)
                {
                    Console.Write("Please enter the answer to {0} x {1} = ?", num01, num02);
                    int product = num01 * num02;
                    Console.WriteLine();
                    int answer = Convert.ToInt32(Console.ReadLine());

                    while (product != answer)
                    {
                        Console.WriteLine("Incorrect, enter another guess: ");
                        answer = Convert.ToInt32(Console.ReadLine());
                    }
                    Console.WriteLine("Correct. Your answer was {0} \n {1} x {2} = {3} Very good!",
                        answer, num01, num02, product);
                    //keep console open
                    Console.WriteLine();
                    Console.WriteLine("Press - 1 to exit");
                    value = Convert.ToInt32(Console.ReadLine());
                }
                else
                {
                    break;
                }
            }
        }
    }

Put the random number generation inside the loop, not before it.

        int value = 0;
        while (true)
        {
            if (value != -1)
            {
                int num01 = randomNumbers.Next(1, 11);
                int num02 = randomNumbers.Next(1, 11);
                ...

The random number generator statement had to be in the loop. And I needed to tweak the console.writeline to include "type 1 to continue". So the final code is:

public class Multiplication
{
    public static void Main(string[] args)
    {
        int value = 1;
        while (true)
        {
            if (value == -1)
            {
                break;
            }
            else
                {
                    Random randomNumbers = new Random(); // random number generator
                    int num01 = randomNumbers.Next(1, 11);
                    int num02 = randomNumbers.Next(1, 11);
                    Console.Write("Please enter the answer to {0} x {1} = ?", num01, num02);
                    int product = num01 * num02;
                    Console.WriteLine();
                    int answer = Convert.ToInt32(Console.ReadLine());

                    while (product != answer)
                    {
                        Console.WriteLine("Incorrect, enter another guess: ");
                        answer = Convert.ToInt32(Console.ReadLine());
                    }
                    Console.WriteLine("Correct. Your answer was {0} \n {1} x {2} = {3} Very good!",
                        answer, num01, num02, product);
                    //keep console open
                    Console.WriteLine();
                    Console.WriteLine("Press - 1 to exit or 1 to continue");
                    value = Convert.ToInt32(Console.ReadLine());
                }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM