2013-10-06 48 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