将两个随机生成的数字相乘直到满足条件

时间:2019-02-08 10:57:07

标签: c# while-loop

我正在尝试制作一个将两个随机生成的数字相乘的程序。 它应该运行乘法过程,直到答案大于269。

我认为while循环有问题。当我运行该程序时,它会不断乘以相同的数字,但答案一直在增加,直到达到268,然后该程序终止。

using System;

namespace ConsoleApp6
{
    class Program
    {
        static void Main(string[] args)
        {
            Random randomelyGeneratedNumbers = new Random();
            int num1 = randomelyGeneratedNumbers.Next(1, 25);
            int num2 = randomelyGeneratedNumbers.Next(1, 9);
            int answer = num1 * num2;
            //  Console.WriteLine(num1+" x "+num2+" is : "+answer);

            while(answer < 269)
            {
                Console.WriteLine(num1+" x "+num2+" is : "+answer);
                answer++;
            }


        }
    }
}

1 个答案:

答案 0 :(得分:2)

您可以直接

static void Main(string[] args)
{
    Random randomelyGeneratedNumbers = new Random();
    int answer = 0;
    while(answer < 269)
    {
        num1 = randomelyGeneratedNumbers.Next(1, 30);
        num2 = randomelyGeneratedNumbers.Next(1, 11);
        answer = num1 * num2;
        if (answer < 296) {
           Console.WriteLine(num1+" x "+num2+" is : "+answer);
        }
    }
}