Java中的递归方法似乎只是“goto”该方法的第一行而不是实际进入下一个调用
我正在创建一个制造房间的工厂,它传递了一系列步骤和一个起始房间,它应该执行一个步骤,构建一个房间,然后用更少的步骤调用自己,并将新房间作为起始房间。问题是它永远不会结束。在调试器中,我可以看到它正在调用自身,这会在内存中创建另一个方法调用,该方法调用实际上少了一步,但随后执行行转到当前方法调用的顶部!所以它实际上从未完成新的调用。就好像它将新的调用放入堆而不是堆栈中,然后从未真正到达它。
代码:
@Override
public Room place(Level level, int cycles, Room start_room,
Direction direction, int shop, int exit, LevelFactoryReport report) throws Exception
{
Room room = null;
if(cycles < 1)
{
return start_room;
}
else
{
report.addEvent("--Placer step--");
report.addEvent("Steps remaining: "+cycles);
room = this.Step(level, start_room, direction, shop, exit, report);
if(room == null)
{
cycles = 0;
report.addEvent("Step returned a null room (probably because it ran into an existing room). Ending cycle.");
}
}
return place(level, (cycles--), room, direction, (shop--), (exit--), report);
}
在上面的代码中,它经历了各种实现,然后到达对 place() 的新调用,然后它只是创建了 place() 的新实例,但是没有步骤进入其中,执行行返回到原始调用的“Room room = start_room”。它无限地执行此操作,循环始终为其初始值 4,并且越来越多的 place() 实例填满堆栈。我查看了新实例,所有实例的“cycles”值实际上都为 3。
奇怪的是,实际运行的每个迭代都在下一个房间上运行,因此当它返回到顶部时,它正要经过下一个房间回到顶部。但为什么它创建一个新的 place() 实例(使用新房间和新周期值 3),然后使用新房间而不是新周期值 3 重新运行旧的 place() ?
I am creating a factory that makes rooms, and it is passed an int of steps and a start room and it is supposed to do a step, build a room, and then call itself with one fewer step and the new room as the start room. The problem is that it never ends. In the debugger, I can see that it's calling itself, which creates another method call in memory that actually has one fewer step, but then the execution line goes to the top of the current method call! so it never actually completes the new call. As though it were putting the new call into heap instead of stack, and then never actually getting to it.
Code:
@Override
public Room place(Level level, int cycles, Room start_room,
Direction direction, int shop, int exit, LevelFactoryReport report) throws Exception
{
Room room = null;
if(cycles < 1)
{
return start_room;
}
else
{
report.addEvent("--Placer step--");
report.addEvent("Steps remaining: "+cycles);
room = this.Step(level, start_room, direction, shop, exit, report);
if(room == null)
{
cycles = 0;
report.addEvent("Step returned a null room (probably because it ran into an existing room). Ending cycle.");
}
}
return place(level, (cycles--), room, direction, (shop--), (exit--), report);
}
In the code above, it goes through the various implementation, then gets to the new call for place(), and then it just creates a new instance of place(), but doesn't step into it, and instead the execution line goes back to "Room room = start_room" of the original call. It does this infinitely, with the cycles always at its initial value of 4, and more and more instances of place() filling up the stack. I looked into the new instances, and all of them actually do have a "cycles" value of 3.
The strange thing is, each iteration that actually runs is being run on the next room, so when it goes back to the top, it is going back to the top passing the next room. But why is it creating a new instance of place() (with the new room AND the new cycles value of 3), and then re-running the old place() using the new room BUT NOT the new cycles value of 3?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在使用
cycles--
、shop--
来减少变量。然而,虽然x--
确实递减 x,但它不会返回递减后的值。表达式x--
的返回值是x
的旧值。使用x-1
而不是x--
。 (如果必须的话,也可以使用--x
,但在这里改变变量是没有意义的)。You're using
cycles--
,shop--
to decrement the variables. However whilex--
does decrement x, it does not return the decremented value. The return value of the expressionx--
is the old value ofx
. Usex-1
instead ofx--
. (Or--x
if you must, but there is no point in mutating the variable here).尝试将此行: 替换
为此行:
也许您可以在此处找到更多帮助
try replacing this line:
with this line:
Maybe you can find some more help here