我如何从递归方法 return 布尔值?
How do i return boolean from recursive method?
此方法在给定迷宫中找到从左上角到右下角的路径。我已经检查过,我的方法为我找到了一条路径,但完成后我无法将其变为 return true。它从我的 if 语句中打印出 "You made it",但它表明 return 为真。
if((x0 == x1) && (y0 == y1)) {
System.out.println(l);
System.out.println("You made it");
return true;
}
我知道这是递归的东西,你 return 以不同的方式取值。我仍然不知道如何正确 return 我的值。
方法如下:
public static boolean findPath(int x0, int y0, int x1, int y1, Labyrinth l) {
l.setMark(x0, y0, true);
if((x0 == x1) && (y0 == y1)) {
System.out.println(l);
System.out.println("You made it");
return true;
}
//is it possible to move in any new direction? if yes, then move
if(l.canMove(Labyrinth.Direction.RIGHT, x0, y0) && !l.getMark(x0+1, y0) && !hasBeen[x0+1][y0]){
findPath(x0+1, y0, x1, y1, l);
}else if(l.canMove(Labyrinth.Direction.DOWN, x0, y0) && !l.getMark(x0, y0+1)&& !hasBeen[x0][y0+1]){;
findPath(x0, y0+1, x1, y1, l);
}else if(l.canMove(Labyrinth.Direction.UP, x0, y0) && !l.getMark(x0, y0-1)&& !hasBeen[x0][y0-1]){
findPath(x0, y0-1, x1, y1, l);
}else if(l.canMove(Labyrinth.Direction.LEFT, x0, y0) && !l.getMark(x0-1,y0)&& !hasBeen[x0-1][y0]){
findPath(x0-1, y0, x1, y1, l);
}else{
//go back one step and set hasBeen true for this coordinate
l.setMark(x0,y0,false);
hasBeen[x0][y0]=true;
if(l.getMark(x0+1, y0)){
findPath(x0+1, y0, x1, y1, l);
}else if(l.getMark(x0, y0+1)){
findPath(x0, y0+1, x1, y1, l);
}else if(l.getMark(x0, y0-1)){
findPath(x0, y0-1, x1, y1, l);
}else if(l.getMark(x0-1,y0)){
findPath(x0-1, y0, x1, y1, l);
}
}
return false;
}
}
您需要传播 return 语句。您需要做的不是递归地调用 findPath(x0+1, y0, x1, y1, l);
,而是:
return findPath(x0+1, y0, x1, y1, l);
此外,您可以取消所有 'else' 语句。只要 if 就足够了。
此方法在给定迷宫中找到从左上角到右下角的路径。我已经检查过,我的方法为我找到了一条路径,但完成后我无法将其变为 return true。它从我的 if 语句中打印出 "You made it",但它表明 return 为真。
if((x0 == x1) && (y0 == y1)) {
System.out.println(l);
System.out.println("You made it");
return true;
}
我知道这是递归的东西,你 return 以不同的方式取值。我仍然不知道如何正确 return 我的值。
方法如下:
public static boolean findPath(int x0, int y0, int x1, int y1, Labyrinth l) {
l.setMark(x0, y0, true);
if((x0 == x1) && (y0 == y1)) {
System.out.println(l);
System.out.println("You made it");
return true;
}
//is it possible to move in any new direction? if yes, then move
if(l.canMove(Labyrinth.Direction.RIGHT, x0, y0) && !l.getMark(x0+1, y0) && !hasBeen[x0+1][y0]){
findPath(x0+1, y0, x1, y1, l);
}else if(l.canMove(Labyrinth.Direction.DOWN, x0, y0) && !l.getMark(x0, y0+1)&& !hasBeen[x0][y0+1]){;
findPath(x0, y0+1, x1, y1, l);
}else if(l.canMove(Labyrinth.Direction.UP, x0, y0) && !l.getMark(x0, y0-1)&& !hasBeen[x0][y0-1]){
findPath(x0, y0-1, x1, y1, l);
}else if(l.canMove(Labyrinth.Direction.LEFT, x0, y0) && !l.getMark(x0-1,y0)&& !hasBeen[x0-1][y0]){
findPath(x0-1, y0, x1, y1, l);
}else{
//go back one step and set hasBeen true for this coordinate
l.setMark(x0,y0,false);
hasBeen[x0][y0]=true;
if(l.getMark(x0+1, y0)){
findPath(x0+1, y0, x1, y1, l);
}else if(l.getMark(x0, y0+1)){
findPath(x0, y0+1, x1, y1, l);
}else if(l.getMark(x0, y0-1)){
findPath(x0, y0-1, x1, y1, l);
}else if(l.getMark(x0-1,y0)){
findPath(x0-1, y0, x1, y1, l);
}
}
return false;
}
}
您需要传播 return 语句。您需要做的不是递归地调用 findPath(x0+1, y0, x1, y1, l);
,而是:
return findPath(x0+1, y0, x1, y1, l);
此外,您可以取消所有 'else' 语句。只要 if 就足够了。