简体   繁体   English

Java中的递归方法似乎只是“转到”方法的第一行,而不是实际进入下一个调用

[英]Recursive method in Java seems to just “goto” the first line of the method instead of actually go into the next call

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. 在上面的代码中,它经历了各种实现,然后到达对place()的新调用,然后仅创建了一个place()的新实例,但没有介入其中,而是执行行回到原始通话的“ Room room = start_room”。 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. 它无限地执行此操作,周期始终保持在其初始值4,并且越来越多的place()实例填充了堆栈。 I looked into the new instances, and all of them actually do have a "cycles" value of 3. 我查看了新实例,它们实际上的“周期”值均为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? 但是,为什么要创建一个新的place()实例(新房间和新周期值为3),然后使用新房间而不是新周期值3重新运行旧的place()呢?

You're using cycles-- , shop-- to decrement the variables. 您正在使用cycles-- shop--来减少变量。 However while x-- does decrement x, it does not return the decremented value. 但是,尽管x--确实减小了x,但它不返回减小后的值。 The return value of the expression x-- is the old value of x . 表达式的返回值x--是旧值x Use x-1 instead of x-- . 使用x-1而不是x-- (Or --x if you must, but there is no point in mutating the variable here). (或者,如果需要,则使用--x ,但是在此处对变量进行更改没有任何意义)。

try replacing this line: 尝试替换此行:

return place(level, (cycles--), room, direction, (shop--), (exit--), report);

with this line: 用这一行:

return place(level, (--cycles), room, direction, (--shop), (--exit), report);

Maybe you can find some more help here 也许您可以在这里找到更多帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM