Java 无限递归

Java 无限递归,java,recursion,Java,Recursion,我有以下代码: public void generateTree(Node myNode) { for(int i = 1; i <= 6; i++) { //Creating child node Node child = new Node(); //Setting new Depth child.setDepth(myNode.getDepth()+1); //Adding node to tr

我有以下代码:

public void generateTree(Node myNode) {
    for(int i = 1; i <= 6; i++) {
        //Creating child node
        Node child = new Node();

        //Setting new Depth
        child.setDepth(myNode.getDepth()+1);

        //Adding node to tree
        myTree.add(child);

        //If the game isn't over and we're not reached the maximum depth, recurse
        if(!isGameOver() && child.getDepth() < MAX_DEPTH)
            generateTree(child);
    }
}
public void generateTree(节点myNode){

对于(inti=1;i您的问题不是无限递归。它可能是其他问题。此代码适用于我-

import java.util.ArrayList;
import java.util.List;


public class Node 
{

    private int depth;

    public static final int MAX_DEPTH = 2;

    private static List<Node> myTree = new ArrayList<Node>(); // for testing purposes

    public void generateTree(Node myNode) {
        for(int i = 1; i <= 6; i++) {
            //Creating child node
            Node child = new Node();

            //Setting new Depth
            child.setDepth(myNode.getDepth()+1);

            //Adding node to tree
            myTree.add(child);

            //If the game isn't over and we're not reached the maximum depth, recurse
            if(child.getDepth() < MAX_DEPTH)
                generateTree(child);
        }
    }

    public static void main(String [] args)
    {
        Node node = new Node();

        Node myNode = new Node();
        myNode.setDepth(0);

        node.generateTree(myNode);

        System.out.println(myTree.size());

    }

    public int getDepth() {
        return depth;
    }

    public void setDepth(int depth) {
        this.depth = depth;
    }

}

您是否尝试过使用print语句获取深度值?或者使用调试器?如果深度不是每一个子级别上升1,那么这就可以解释无限递归。什么是
MAX_Depth
?定义无限递归-它是永远持续还是抛出
StackOverflowException
?Dukeling我已经将MAX_Depth设置为事实是,如果级别为2,则不需要检查超过36个案例…@Dario Panada您需要发布所有代码,或者您需要使用打印/调试来自己解决问题。我们不是通灵者:)Put
“System.out.println(“start=“+myNode.getDepth()”)
在第一行和设置深度(或可能包括标识节点的唯一ID)后的“System.out.println”(“child=“+child.getDepth())”
)。这将使您合理地了解出了什么问题。
42