2013-10-06 44 views
1

我在java中有一个奇怪的问题与if-else语句。下面是一个递归方法,试图找到一个名为getPathThroughMaze的迷宫的末尾。如果在java语句行为奇怪

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)) 

将评估为假,我会(后也许在调试器上点击几步骤)在

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

到哪我就可以检查条件(我认为这是真的),并继续穿越迷宫。而实际上,当我点击跨过上

if(!visited.contains(nextPoint)) 

调试器(在elcipse和IntelliJ)移动一直到我的方法的最后一个return语句,并想要返回“”。我不明白为什么我所有其他的陈述都被跳过了。任何人都可以启发我,为什么可能是这种情况?如果我的解释不够清楚,请让我知道。

+2

你的代码本身的的if/else一个迷宫。我强烈建议改变设计。这是非常难以调试的。 – Lokesh

回答

2

If/else陈述是排他性的,因此您不会到达else if (current == ' ' && (down == '_' || down == ' ')),因为您已经进入else if (up == ' ')分支。由于if(!visited.contains(nextPoint))内部的if为false,程序进入else部分,//do nothing注释并且什么都不做(实际上,你不需要也不应该写一个空的else语句,至少要添加一个log语句给它使其更易于调试)。然后它退出if/else区块并前往return

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

I.e.而不是:

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

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

哦,哇......我是个白痴。谢谢。 – user2833546

+0

@ user2833546,不客气:) – svz

+0

+1好的解释。 – Sello