java - 递归函数JAVA中的返回值

标签 java search return

我设计了一个递归调用自身的函数。但是 return 语句并没有做我想要它做的事情。我们已经用 print 检查了是否已经返回,但它并没有返回到初始函数。 它输入的语句:

if(depth==0 && pb.isGoalState()){
            System.out.println("!!!!!WOOOOOW!!!!!");
            return pb;
}

println 显示正常,但是当返回 pb 时,事情变得很奇怪。

回到函数的时候:

result = DLS(pb,depth); //never returns here!!!
System.out.println("Here: "+result.toString());

它永远不会打印出上面的打印件。我看不出有什么问题!我检查了我自己设计的其他方法。

private puzzleBoard IDS(String initial){
        puzzleBoard pb = new puzzleBoard(initial,0,new Vector<Integer>(),new Vector<puzzleBoard>(),new Vector<puzzleBoard>());
        int depth=0;
        puzzleBoard result=new puzzleBoard("999999999",0,new Vector<Integer>(),new Vector<puzzleBoard>(),new Vector<puzzleBoard>());
        while(true){//Repeat
            System.out.println("DP "+depth);
            result = DLS(pb,depth);
            System.out.println("Here: "+result.toString());
            if(result.isGoalState())
                return result;
            depth++;
        }

        }

    private puzzleBoard DLS(puzzleBoard pb, int depth){
        System.out.println("AVskilj depth "+depth+" "+(depth==0 && pb.isGoalState()));
        pb.printPuzzle();
        if(depth==0 && pb.isGoalState()){
            System.out.println("!!!!!WOOOOOW!!!!!");
            return pb;
        }
        else if(depth>0){
            for(Iterator<puzzleBoard> child = generateSuccessorsIDS(pb).iterator(); child.hasNext();){
                puzzleBoard tmp;
                tmp=child.next();
                tmp.printPuzzle();
                DLS(tmp,(depth-1));
            }

        }
        else
            return new puzzleBoard("999999999",0,new Vector<Integer>(),new Vector<puzzleBoard>(),new Vector<puzzleBoard>());
        return pb;
        }

所以我的问题现在还在这部分代码中

for(Iterator<puzzleBoard> child = generateSuccessorsIDS(pb).iterator(); child.hasNext();){
                DLS(child.next(),(depth-1));
            }

当我不使用 DLS(child.next(),(depth-1)) 之前的 return 时;它按预期遍历每个 child ,但由于缺少返回值而没有存储值。当我在它之前使用 return 时,它只是通过迭代器中的第一个子项并忽略其余部分,因为 return 语句终止循环。

如何解决?我也想不出别的办法。

最佳答案

在这次迭代中:

   for(Iterator<puzzleBoard> child = generateSuccessorsIDS(pb).iterator(); child.hasNext();){
                puzzleBoard tmp;
                tmp=child.next();
                tmp.printPuzzle();
                DLS(tmp,(depth-1));
            }

看线:

DLS(tmp,(depth-1));

DLS 返回一个 puzzleBoard 对象,但您不使用从此行返回的对象,因此返回的递归对象将被忽略。我没有验证您方法的更正,但您应该从这里开始。顺便说一句,如果子板的数量很大,这个函数可能会花费很长时间,因为你在每个子板上调用它。

编辑:这是您如何处理从 DLS 退回的电路板的示例:

 else if(depth>0){
       for(Iterator<puzzleBoard> child = generateSuccessorsIDS(pb).iterator(); child.hasNext();){
                    puzzleBoard tmp;
                    tmp=child.next();
                    tmp.printPuzzle();
                    puzzleBoard resultPB = DLS(tmp,(depth-1));

                    // mergre resultPB with current puzzle board (e.g. pb.addChild(resultPB));
                }

       return pb;
}

关于java - 递归函数JAVA中的返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10063350/

相关文章:

php - PHP解析错误: syntax error, unexpected ',' in

android - 在 Realm Recycler View Android 中搜索 View

macos - 原生 OS X 应用程序是否可以读取和复制 Spotlight 搜索索引?

c - gettoken 函数 - 程序不明确 (K&R)

go - 为什么要命名返回参数?

java - 搜索 ArrayList

java - 如何获取Java中main中打印的类方法的返回值?

java - 解析json文件丢失的数据结构 - JAVA

java - 具有用户特定功能的 JSF Web 应用程序

java - 捕获特定的异常并在所有异常下执行相同的操作?