1

我对 java 中的 if-else 语句有一个奇怪的问题。下面是一个递归方法,它试图找到名为 getPathThroughMaze 的迷宫的尽头。

private static String getPathThroughMaze(char[][] maze, Set<Point> visited, Point currentPoint) {
    int currentX = currentPoint.x;
    int currentY = currentPoint.y;
    visited.add(currentPoint);

    //end case. append '!' so we know which path leads to the end of the maze
    if (currentX == (xLength - 2) && currentY == (yLength - 1)) {
        return "!";
    }

    char left = maze[currentY][currentX - 1];
    char right = maze[currentY][currentX + 1];
    char up = maze[currentY - 1][currentX];
    char down = maze[currentY + 1][currentX];
    char current = maze[currentY][currentX];

    /* If valid, visit all non-visited adjacent squares.
       Only odd numbered columns will be traversed
       since even numbered columns represent vertical wall
       columns
     */
    if (right == '_' || right == ' ') {
        Point nextPoint = new Point(currentX + 2, currentY);
        if (!visited.contains(nextPoint)) {
            String path = "E" + getPathThroughMaze(maze, visited, nextPoint);
            if (path.endsWith("!")) {
                return path;
            } else {
                //do nothing.
            }
        }else {
            //do nothing.
        }
    } else if (up == ' ') {
        Point nextPoint = new Point(currentX, currentY - 1);
        if (!visited.contains(nextPoint)) {
            String path = "N" + getPathThroughMaze(maze, visited, nextPoint);
            if (path.endsWith("!")) {
                return path;
            } else {
                //do nothing.
            }
        } else {
            //do nothing.
        }
    } else if ( current == ' ' && (down == '_' || down == ' ')) {
        Point nextPoint = new Point(currentX, currentY + 1);
        if (!visited.contains(nextPoint)) {
            String path = "S" + getPathThroughMaze(maze, visited, nextPoint);
            if (path.endsWith("!")) {
                return path;
            } else {
                //do nothing.
            }
        } else {
            //do nothing.
        }
    } else if (left == '_' || left == ' ') {
        Point nextPoint = new Point(currentX - 2, currentY);
        if (!visited.contains(nextPoint)) {
            String path = "W" + getPathThroughMaze(maze, visited, nextPoint);
            if (path.endsWith("!")) {
                return path;
            } else {
                //do nothing.
            }
        } else {
            //do nothing.
        }
    } else {
      return "";  
    }
    //otherwise...
    return "";
}

在递归中遇到问题的地方,变量是:

currentX = 3
currentY = 2
right = '|'
left = '|'
up = ' '
down = '_'
current = ' '
visited contains points (1,1), (3,1), (3,2)

在第一个 else if 语句中:

else if (up == ' ')

创建一个新点 (3,1),该点已包含在访问集中。我期望发生的是

if(!visited.contains(nextPoint))

将评估为 false 并且我将(可能在调试器中单击几下之后)到达

else if ( current == ' ' && (down == '_' || down == ' ')) 

然后我可以检查那个条件(我希望它是真的)并继续穿越迷宫。实际发生的是当我单击 step over on

if(!visited.contains(nextPoint))

调试器(在 elcipse 和 intellij 中)一直移动到我的方法的最后一个 return 语句,它想要返回“”。我不明白为什么我的所有其他 if else 语句都被跳过了。谁能告诉我为什么会这样?如果我的解释不够清楚,请告诉我。

4

1 回答 1

2

If/else声明是独家的,所以你不会到达,else if ( current == ' ' && (down == '_' || down == ' '))因为你已经进入了else if (up == ' ')分支。因为if(!visited.contains(nextPoint))innerif是假的,程序进入它的else部分并带有//do nothing注释并且什么都不做(实际上,你不需要也不应该写一个空else语句。至少在它上面添加一个日志语句以便于调试)。然后它退出if/else块并转到return

如果您希望您的代码检查每个if/else方法调用的每个分支,只需将其替换为一些简单的if语句。

即代替:

if (condition1){
} else if (condition2){
} else if (condition3){
}

if (condition1){
} 
if (condition2){
} 
if (condition3){
}
于 2013-10-06T06:18:48.090 回答