我相信这些特定的代码行是导致错误的原因。
这有什么工作吗? z保存grid类型对象的静态int变量的值。对于此代码,有一个随机大小的网格。在代码的开头,单个网格点“走”到另外两个网格点,然后每个网格点“走”到另外两个网格点,依此类推。
假设在有3000次散步时停止递归。每次网格点“走路”时,步行的数量都会更新。因此,遍历数是坐标类的静态变量,坐标类是组成每个网格点的对象类型。
坐标只不过是和x和y组件。 “int a”和“int b”是分别在Recurs方法中接受“x”和“y”网格组件的参数。 x和y分量在recursiveWalk方法中随机分配。
//Walks
public void Recurs(int a, int b)
{
grid[a, b].updateAccessed();
grid[a, b].setWalkCount();
int z = grid[a, b].getWalkCount();
if (z == 3000)
{
return;
}
else
{
int one = rand.Next(20);
int two = rand.Next(20);
//int three = rand.Next(20);
int four = rand.Next(25);
int five = rand.Next(25);
//int six = rand.Next(25);
Recurs(one, four);
Recurs(two, five);
//Recurs(three, six);
}
}
//Walks to any grid point recursively
public void RecursiveWalk()
{
int x = rand.Next(20);
int y = rand.Next(25);
Recurs(x, y);
}
答案 0 :(得分:1)
你在每次递归中调用Recurs两次?你能改变一下情况吗?
//Walks
public void Recurs(int a, int b)
{
grid[a, b].updateAccessed();
grid[a, b].setWalkCount();
int z = grid[a, b].getWalkCount();
//i assume in 1 of your recursion,
//the count was set to 3001 and it continues
if (z >= 3000) //<-- >= instead of =
{
return;
}
else
{
int one = rand.Next(20);
int two = rand.Next(20);
//int three = rand.Next(20);
int four = rand.Next(25);
int five = rand.Next(25);
//int six = rand.Next(25);
Recurs(one, four);
Recurs(two, five);
//Recurs(three, six);
}
}
//Walks to any grid point recursively
public void RecursiveWalk()
{
int x = rand.Next(20);
int y = rand.Next(25);
Recurs(x, y);
}
答案 1 :(得分:0)
我根本没有看到使用递归的理由。你能简单地使用一个循环吗?
public void IterativeWalk()
{
for (int z=0; z<3000; z++)
{
int x = rand.Next(20);
int y = rand.Next(25);
grid[x, y].updateAccessed();
grid[x, y].setWalkCount();
}
}