【发布时间】:2012-10-01 06:52:28
【问题描述】:
我在执行此递归时收到 stackoverflow 错误。 有一种模式,它先说一次:
在 MazeGui.move(MazeGui.java:79) 这是行 if(rigting.goal == 真的){
然后它说以下两个,并且它们都在输出中重复了很长时间。问题发生在某个地方,我只是不确定是什么:
在 MazeGui.move(MazeGui.java:89) 这是线移动(rigting.right, 位置); //向右移动
在 MazeGui.move(MazeGui.java:107) 这是线移动(rigting.left, 位置); //向左移动
...
...
我是否缺少终止条件或其他内容,是否发生了无限递归?我无法将我的头包裹起来,完全迷失了。任何帮助将不胜感激。
代码:
public boolean move(Maze rigting, int pos)
{
if (rigting.goal == true)
{
return true;
}
if (rigting.wallR != true)
{
pos += 1;
move(rigting.right, pos); //moves right
showLabel(pos);
return true;
}
if(rigting.wallD != true) //checks if there is a wall below
{
pos += 10;
move(rigting.down, pos); //moves down
showLabel(pos);
return true;
}
if(rigting.wallL != true) //checks if there is a wall on the left
{
pos -= 1;
move(rigting.left, pos); //moves left
showLabel(pos);
return true;
}
if(rigting.wallU != true) //checks if there is a wall above
{
pos -= 10;
move(rigting.up, pos); //moves up
showLabel(pos);
return true;
}
return false;
}
【问题讨论】:
-
是的。堆栈溢出通常意味着不满足停止条件。
标签: java recursion stack-overflow