为什么这些 while 循环不起作用?还是其他问题?
Why are these while loops not working? Or is something else the problem?
我最近才开始使用 C#,因为大学的课程正在使用这种语言。
[通过将 1 到 30 之间的三个数字与按相同顺序抽取的三个随机数字相匹配,即可赢得彩票中的头等奖。
当一个球被抽出时,它会在抽出另一个球之前被放回机器中。在抽取一个球之前,机器中总是有 30 个球,玩家可以多次选择同一个球。
每周抽奖一次。编写一个算法,将三个数字作为输入,重复抽取三个介于 1 和 30 之间的随机数,直到出现三个匹配和 returns 赢得大奖所需的周数。--这是我必须的程序创造。没有图片是不是更好。当我执行这段代码时,它会无限运行并且没有给我一个具体的答案。
][1]
使用系统;
命名空间挑战
{
class 程序
{
静态无效主要(字符串[]参数)
{
// 彩票问题
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);
问题是您必须将随机 selection 代码传递到 loop.I 中,意思是“那行 select 随机数”。
我最近才开始使用 C#,因为大学的课程正在使用这种语言。 [通过将 1 到 30 之间的三个数字与按相同顺序抽取的三个随机数字相匹配,即可赢得彩票中的头等奖。 当一个球被抽出时,它会在抽出另一个球之前被放回机器中。在抽取一个球之前,机器中总是有 30 个球,玩家可以多次选择同一个球。 每周抽奖一次。编写一个算法,将三个数字作为输入,重复抽取三个介于 1 和 30 之间的随机数,直到出现三个匹配和 returns 赢得大奖所需的周数。--这是我必须的程序创造。没有图片是不是更好。当我执行这段代码时,它会无限运行并且没有给我一个具体的答案。
][1]
使用系统;
命名空间挑战 { class 程序 { 静态无效主要(字符串[]参数) { // 彩票问题
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);
问题是您必须将随机 selection 代码传递到 loop.I 中,意思是“那行 select 随机数”。