我最近才开始使用 c#,因为这种语言正在用于大学课程。[彩票中的头奖是通过将 1 到 30 之间的三个数字与以相同顺序抽取的三个随机数字相匹配来中奖的。当一个球被抽出时,它会在另一个球被抽出之前放回机器中。在抽出一个球之前,机器中总是有 30 个球,玩家可以多次选择同一个球。每周进行一次抽奖。编写一个算法,将三个数字作为输入,重复抽取 1 到 30 之间的三个随机数,直到出现三场比赛,然后返回赢得大奖所需的周数。——这是我必须创建的程序。没有图会不会好点。当我执行这段代码时,它会无限运行并且没有给我一个具体的答案。
][1]
使用系统;
命名空间挑战 { class Program { static void Main(string[] args) { // 彩票问题
double num1, num2, num3;
int week = 0;
Console.Write("Enter the first number: ");
num1 = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter the second number: ");
num2 = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter the third number: ");
num3 = Convert.ToDouble(Console.ReadLine());
Random rnd = new Random();
int ball1 = rnd.Next(1, 31);
int ball2 = rnd.Next(1, 31);
int ball3 = rnd.Next(1, 31);
do
{
week++;
do
{
Console.WriteLine("it took " + week + " weeks to win the jackpot");
break;
} while (ball1 == num1 && ball2 == num2 && ball3 == num3);
} while (ball1 != num1 || ball2 != num2 || ball3 != num3);