重复调用函数,直到收到所需的输出
Call function repeatedly until desired output is received
我正在制作一种自动统计滚轮,我希望它保持 运行ning 直到我获得所需的输出。这是代码:
class Program
{
static void Main(string[] args)
{
_getStats();
}
static void _getStats()
{
Random rnd = new Random();
int stat1 = 0;
int stat2 = 0;
int stat3 = 0;
int stat4 = 0;
int stat5 = 0;
int stat6 = 0;
for (int i = 0; i < 6; i++)
{
int roll1 = rnd.Next(1, 7);
int roll2 = rnd.Next(1, 7);
int roll3 = rnd.Next(1, 7);
int roll4 = rnd.Next(1, 7);
int tempStatTotal = 0;
List<int> rollList = new List<int>();
rollList.Add(roll1);
rollList.Add(roll2);
rollList.Add(roll3);
rollList.Add(roll4);
rollList.Sort();
rollList.RemoveAt(0);
foreach (int j in rollList)
{
tempStatTotal += j;
}
if (i == 0)
{
stat1 = tempStatTotal;
}
else if(i == 1)
{
stat2 = tempStatTotal;
}
else if (i == 2)
{
stat3 = tempStatTotal;
}
else if (i == 3)
{
stat4 = tempStatTotal;
}
else if (i == 4)
{
stat5 = tempStatTotal;
}
else
{
stat6 = tempStatTotal;
}
}
if(stat1 == 18 || stat2 == 18 || stat3 == 18 || stat4== 18 || stat5 == 18 || stat6 == 18)
{
Console.WriteLine(stat1);
Console.WriteLine(stat2);
Console.WriteLine(stat3);
Console.WriteLine(stat4);
Console.WriteLine(stat5);
Console.WriteLine(stat6);
}
else
{
Main(null); //Start the process all over again until one of the stats is 18
}
}
}
当我 运行 这段代码时,在某些情况下,控制台会记录 6 个数字,其中 1 个是 18。大多数时候,应用程序中断并抛出错误 System.WhosebugException
.我知道这与无限循环有关并且它正在发生,因为我正在递归调用我的方法,但我该如何解决这个问题?
使用 do-while 循环:
class Program
{
static void Main(string[] args)
{
_getStats();
}
static void _getStats()
{
do
{
Random rnd = new Random();
int stat1 = 0;
int stat2 = 0;
int stat3 = 0;
int stat4 = 0;
int stat5 = 0;
int stat6 = 0;
for (int i = 0; i < 6; i++)
{
int roll1 = rnd.Next(1, 7);
int roll2 = rnd.Next(1, 7);
int roll3 = rnd.Next(1, 7);
int roll4 = rnd.Next(1, 7);
int tempStatTotal = 0;
List<int> rollList = new List<int>();
rollList.Add(roll1);
rollList.Add(roll2);
rollList.Add(roll3);
rollList.Add(roll4);
rollList.Sort();
rollList.RemoveAt(0);
foreach (int j in rollList)
{
tempStatTotal += j;
}
if (i == 0)
{
stat1 = tempStatTotal;
}
else if (i == 1)
{
stat2 = tempStatTotal;
}
else if (i == 2)
{
stat3 = tempStatTotal;
}
else if (i == 3)
{
stat4 = tempStatTotal;
}
else if (i == 4)
{
stat5 = tempStatTotal;
}
else
{
stat6 = tempStatTotal;
}
}
if (stat1 == 18 || stat2 == 18 || stat3 == 18 || stat4 == 18 || stat5 == 18 || stat6 == 18)
{
Console.WriteLine(stat1);
Console.WriteLine(stat2);
Console.WriteLine(stat3);
Console.WriteLine(stat4);
Console.WriteLine(stat5);
Console.WriteLine(stat6);
break;
}
} while (true);
}
}
啊,递归。在您获得 WhosebugException 之前,一切都很有趣和游戏。
你是对的,你对递归的使用导致了这个问题。递归是一个有用的工具,但它会在 long-运行 操作期间耗尽你的内存。正如其他人已经建议的那样,使用 while 循环。您可以使用 do-while 循环,但我认为常规循环就可以了。
static void _getStats()
{
Random rnd = new Random();
int stat1 = 0;
int stat2 = 0;
int stat3 = 0;
int stat4 = 0;
int stat5 = 0;
int stat6 = 0;
while (stat1 < 18 || stat2 < 18 || stat3 < 18 || stat4 < 18 || stat5 < 18 || stat6 < 18)
{
// rest of your code
}
// Move your printing after the while loop. Once one of the stats hits 18 or above,
// print the results.
Console.WriteLine(stat1);
Console.WriteLine(stat2);
Console.WriteLine(stat3);
Console.WriteLine(stat4);
Console.WriteLine(stat5);
Console.WriteLine(stat6);
}
我正在制作一种自动统计滚轮,我希望它保持 运行ning 直到我获得所需的输出。这是代码:
class Program
{
static void Main(string[] args)
{
_getStats();
}
static void _getStats()
{
Random rnd = new Random();
int stat1 = 0;
int stat2 = 0;
int stat3 = 0;
int stat4 = 0;
int stat5 = 0;
int stat6 = 0;
for (int i = 0; i < 6; i++)
{
int roll1 = rnd.Next(1, 7);
int roll2 = rnd.Next(1, 7);
int roll3 = rnd.Next(1, 7);
int roll4 = rnd.Next(1, 7);
int tempStatTotal = 0;
List<int> rollList = new List<int>();
rollList.Add(roll1);
rollList.Add(roll2);
rollList.Add(roll3);
rollList.Add(roll4);
rollList.Sort();
rollList.RemoveAt(0);
foreach (int j in rollList)
{
tempStatTotal += j;
}
if (i == 0)
{
stat1 = tempStatTotal;
}
else if(i == 1)
{
stat2 = tempStatTotal;
}
else if (i == 2)
{
stat3 = tempStatTotal;
}
else if (i == 3)
{
stat4 = tempStatTotal;
}
else if (i == 4)
{
stat5 = tempStatTotal;
}
else
{
stat6 = tempStatTotal;
}
}
if(stat1 == 18 || stat2 == 18 || stat3 == 18 || stat4== 18 || stat5 == 18 || stat6 == 18)
{
Console.WriteLine(stat1);
Console.WriteLine(stat2);
Console.WriteLine(stat3);
Console.WriteLine(stat4);
Console.WriteLine(stat5);
Console.WriteLine(stat6);
}
else
{
Main(null); //Start the process all over again until one of the stats is 18
}
}
}
当我 运行 这段代码时,在某些情况下,控制台会记录 6 个数字,其中 1 个是 18。大多数时候,应用程序中断并抛出错误 System.WhosebugException
.我知道这与无限循环有关并且它正在发生,因为我正在递归调用我的方法,但我该如何解决这个问题?
使用 do-while 循环:
class Program
{
static void Main(string[] args)
{
_getStats();
}
static void _getStats()
{
do
{
Random rnd = new Random();
int stat1 = 0;
int stat2 = 0;
int stat3 = 0;
int stat4 = 0;
int stat5 = 0;
int stat6 = 0;
for (int i = 0; i < 6; i++)
{
int roll1 = rnd.Next(1, 7);
int roll2 = rnd.Next(1, 7);
int roll3 = rnd.Next(1, 7);
int roll4 = rnd.Next(1, 7);
int tempStatTotal = 0;
List<int> rollList = new List<int>();
rollList.Add(roll1);
rollList.Add(roll2);
rollList.Add(roll3);
rollList.Add(roll4);
rollList.Sort();
rollList.RemoveAt(0);
foreach (int j in rollList)
{
tempStatTotal += j;
}
if (i == 0)
{
stat1 = tempStatTotal;
}
else if (i == 1)
{
stat2 = tempStatTotal;
}
else if (i == 2)
{
stat3 = tempStatTotal;
}
else if (i == 3)
{
stat4 = tempStatTotal;
}
else if (i == 4)
{
stat5 = tempStatTotal;
}
else
{
stat6 = tempStatTotal;
}
}
if (stat1 == 18 || stat2 == 18 || stat3 == 18 || stat4 == 18 || stat5 == 18 || stat6 == 18)
{
Console.WriteLine(stat1);
Console.WriteLine(stat2);
Console.WriteLine(stat3);
Console.WriteLine(stat4);
Console.WriteLine(stat5);
Console.WriteLine(stat6);
break;
}
} while (true);
}
}
啊,递归。在您获得 WhosebugException 之前,一切都很有趣和游戏。
你是对的,你对递归的使用导致了这个问题。递归是一个有用的工具,但它会在 long-运行 操作期间耗尽你的内存。正如其他人已经建议的那样,使用 while 循环。您可以使用 do-while 循环,但我认为常规循环就可以了。
static void _getStats()
{
Random rnd = new Random();
int stat1 = 0;
int stat2 = 0;
int stat3 = 0;
int stat4 = 0;
int stat5 = 0;
int stat6 = 0;
while (stat1 < 18 || stat2 < 18 || stat3 < 18 || stat4 < 18 || stat5 < 18 || stat6 < 18)
{
// rest of your code
}
// Move your printing after the while loop. Once one of the stats hits 18 or above,
// print the results.
Console.WriteLine(stat1);
Console.WriteLine(stat2);
Console.WriteLine(stat3);
Console.WriteLine(stat4);
Console.WriteLine(stat5);
Console.WriteLine(stat6);
}