C#控制台应用随机数生成

3

如何让随机数生成器方法循环并产生新的随机数?我需要一个单独的类来处理随机数生成吗?如果是这样,我如何将这些变量的作用域传递到主函数Main中?控制台应用程序应该使用循环重复整数数学问题,条件是正确或错误的答案。此外,我忽略了哪种语法来循环回到新的随机生成整数实例?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;
                }
            }
        }
    }
2个回答

6
将随机数生成放在循环内部,而不是在循环之前。
        int value = 0;
        while (true)
        {
            if (value != -1)
            {
                int num01 = randomNumbers.Next(1, 11);
                int num02 = randomNumbers.Next(1, 11);
                ...

是的,我的哨兵控制循环有问题,因为我仍然只得到一个乘法问题的迭代。 - Big T War
@BigTWar:它是否退出循环,还是你得到相同的数字? - Douglas Zare
循环用于错误答案,但在输入正确答案后停止。 - Big T War

1
随机数生成语句必须在循环中。我需要调整控制台输出语句,加上“输入1继续”的提示。最终代码如下:
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());
                }
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接