递归函数JAVA中的返回值
我设计了一个递归调用自身的函数。但是return语句并没有实现我希望它实现的功能。我们已经通过打印检查了返回,但是它没有返回到初始函数。 它输入的声明:递归函数JAVA中的返回值,java,search,return,Java,Search,Return,我设计了一个递归调用自身的函数。但是return语句并没有实现我希望它实现的功能。我们已经通过打印检查了返回,但是它没有返回到初始函数。 它输入的声明: if(depth==0 && pb.isGoalState()){ System.out.println("!!!!!WOOOOOW!!!!!"); return pb; } println显示得很好,但是当pb返回时,事情变得很奇怪 当它返回到函数时: result = DLS
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;
}
专用拼图板ID(字符串首字母){
拼图板pb=新拼图板(初始值,0,新向量(),新向量(),新向量());
int深度=0;
拼图板结果=新拼图板(“99999999”,0,新向量(),新向量(),新向量());
while(true){//重复
系统输出打印项次(“DP”+深度);
结果=DLS(pb,深度);
System.out.println(“此处:+result.toString());
if(result.isGoalState())
返回结果;
深度++;
}
}
私人拼图板DLS(拼图板pb,整数深度){
System.out.println(“AVskilj depth”+depth++(depth==0&&pb.isGoalState());
pb.printPuzzle();
如果(深度==0&&pb.isGoalState()){
System.out.println(“!!!!!呜呜!!!!!!!”);
返回pb;
}
否则,如果(深度>0){
for(Iterator child=generateSuccessorsId(pb.Iterator();child.hasNext();){
拼板tmp;
tmp=child.next();
tmp.printPuzzle();
DLS(tmp(深度1));
}
}
其他的
返回新的拼图板(“99999999”,0,new Vector(),new Vector(),new Vector());
返回pb;
}
所以我的问题仍然在代码的这一部分
for(Iterator<puzzleBoard> child = generateSuccessorsIDS(pb).iterator(); child.hasNext();){
DLS(child.next(),(depth-1));
}
for(Iterator child=generateSuccessorsIDS(pb.Iterator();child.hasNext();){
DLS(child.next(),(depth-1));
}
当我不在DLS之前使用return(child.next(),(depth-1));它按预期遍历每个子级,但由于缺少返回,因此不存储值。当我在前面使用return时,它只会遍历迭代器中的第一个子项,而忽略其余的子项,因为return语句会终止循环
如何解决这个问题?我也想不出其他方法。在这个迭代中:
for(Iterator<puzzleBoard> child = generateSuccessorsIDS(pb).iterator(); child.hasNext();){
puzzleBoard tmp;
tmp=child.next();
tmp.printPuzzle();
DLS(tmp,(depth-1));
}
DLS返回一个puzzboard
对象,但您不使用从此行返回的对象,因此返回的递归对象将被忽略。我没有验证你方法的正确性,但你应该从这里开始。顺便说一句,如果子电路板的数量很大,那么在每个子电路板上调用该函数可能需要很长时间
编辑:这是一个关于如何处理从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;
}
else if(深度>0){
for(Iterator child=generateSuccessorsId(pb.Iterator();child.hasNext();){
拼板tmp;
tmp=child.next();
tmp.printPuzzle();
拼图板结果PB=DLS(tmp,(深度-1));
//带当前拼图板的mergre resultPB(例如pb.addChild(resultPB));
}
返回pb;
}
您还应该在谷歌上搜索java编码约定。谢谢您的意见。问题是,当我在DLS前面使用return(tmp,(depth-1)),它似乎只是计算向量中的第一个childrend。如何解决这个问题?我不知道。也许我只是有点困惑…我不知道拼图板的结构,但我想从DLS返回的板此时应该作为child插入到当前的拼图板对象中。好的,出于实验目的,我创建了一个全局变量(!),但事情仍然没有按预期工作。我打印出来是为了检查这个变量result的值是否正确。紧接着,DLS()应该结束(不返回任何内容),结果应该保持它的值。很明显,它没有,为什么呢?另一个问题是,当我试图返回DLS时,它只计算迭代器中的第一个元素。如何归还全部?没有out return,我似乎无法保存值。同样,您需要管理从DLS返回的所有结果(在迭代循环中),并将它们合并在一起(取决于您的电路板实现),然后返回合并的电路板结果。(看我编辑的answed)
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;
}