我正在制作各种自动状态滚轮,我希望它能继续运行直到我得到所需的输出。这是代码:
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
。我知道这与无限循环有关并且它正在发生,因为我递归调用我的方法,但我该如何解决这个问题呢?
使用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);
}
}
啊,递归。在获得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);
}