我正在制作各种自动统计辊,我希望它继续运行直到获得所需的输出。这是代码:
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.StackOverflowException
。我知道这与无限循环有关,而且之所以会发生是因为我递归地调用了我的方法,但是我该如何解决呢?
答案 0 :(得分:3)
使用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);
}
}
答案 1 :(得分:0)
啊,递归。在获得StackOverflowException之前,这都是有趣的游戏。
您正确的是使用递归引起了这一点。递归是一个有用的工具,但是在长时间运行的操作中,它可能会耗尽您的内存。正如其他人已经建议的那样,请使用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);
}