I'm having a weird issue with if-else statements in java. Below is a recursive method that attempts to find the end of a maze called 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 "";
}
At the point in the recursion where I run into a problem the variables are:
currentX = 3
currentY = 2
right = '|'
left = '|'
up = ' '
down = '_'
current = ' '
visited contains points (1,1), (3,1), (3,2)
In the first else if statement:
else if (up == ' ')
a new point (3,1) is created which IS already contained in the visited set. What I expect to happen is that
if(!visited.contains(nextPoint))
will evaluate to false and that I will (maybe after a few step over clicks in the debugger) arrive at
else if ( current == ' ' && (down == '_' || down == ' '))
where I can then check that condition (which I expect to be true) and continue traversing the maze. What actually happens is when I click step over on
if(!visited.contains(nextPoint))
the debugger (in both elcipse and intellij) moves all the way to the very last return statement of my method and it wants to return "". I don't understand why all my other if else statements are being skipped. Can anyone enlighten me as to why that might be the case? Please let me know if my explanation isn't clear enough.
If/else
statements are exclusive, so you will not arrive to else if ( current == ' ' && (down == '_' || down == ' '))
as you have already entered the else if (up == ' ')
branch. As if(!visited.contains(nextPoint))
in the inner if
is false, the program goes into its else
part with //do nothing
comment and does nothing (actually, you don't need to and shouldn't write an empty else
statement. At least add a log statement to it to make it easier to debug). Then it exits the if/else
block and goes to return
.
If you want your code to check every branch of if/else
on every method call, just replace it with a number of simple if
statements.
Ie instead of:
if (condition1){
} else if (condition2){
} else if (condition3){
}
write
if (condition1){
}
if (condition2){
}
if (condition3){
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.