gpt4 book ai didi

java - java中奇怪的if语句行为

转载 作者:行者123 更新时间:2023-11-30 04:12:52 26 4
gpt4 key购买 nike

我在使用 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”时

if(!visited.contains(nextPoint))

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

最佳答案

If/else 语句是独占的,因此您不会到达 else if ( current == ' ' && (down == '_' || down == ' ') ) 因为您已经进入了 else if (up == ' ') 分支。由于内部 if 中的 if(!visited.contains(nextPoint)) 为 false,因此程序将进入其 else 部分,其中 //do Nothing 注释,什么也不做(实际上,你不需要也不应该写一个空的 else 语句。至少添加一条日志语句以使其更容易进行调试)。然后它退出 if/else block 并转到 return

如果您希望代码在每次方法调用时检查 if/else 的每个分支,只需将其替换为许多简单的 if 语句即可。

即而不是:

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

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

关于java - java中奇怪的if语句行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19205927/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com