我試圖寫遞歸代碼來查找正方形網格的所有退出路徑,並且我在Project4.escape(Project4.java:41) 處得到一些錯誤 我不知道錯誤是什麼是雖然,因爲下面的行:未知的Java遞歸錯誤
在Project4.escape(Project4.java:41)被重複
所以常常該錯誤消息是不可見的。請幫我找到問題,並解決它。接下來是我的逃生方法。
private static void escape(int row, int col, int[][] swamp,String escapePath)
{
if (row == swamp.length || row==0 || col == 0 || col == swamp.length)
{
//winner
System.out.println(escapePath);
}
else
{
for (int i = -1; i < 2; i++)
{
for (int j=-1;j<2;j++)
{
if (row+i>=0 && j+col>=0 && row+i<swamp.length && col+j<swamp.length &&
swamp[row + i][col + j] == 1)
{
escapePath+="["+row+","+col+"]";
swamp[row][col]=2;
escape(row,col,swamp,escapePath);
}
}
}
}
swamp[row][col]=1;
}
//編輯:接下來是現在的主要方法,它顯示了我最初稱爲逃生。
public static void main(String[] args) throws Exception
{
int[] dropInPt = new int[2]; // row and col will be on the 2nd line of input file;
int[][] swamp = loadSwamp(args[0], dropInPt); //returns an 8 by 8 grid of 0s and 1s and fills dropInPt with 1s
int row=dropInPt[0], col = dropInPt[1];
printSwamp( "\n SWAMP: dropped in at: ["+row+","+col+"]\n",swamp);
System.out.println("\n ESCAPE PATHS:\n");
String escapePath = "["+dropInPt[0]+","+dropInPt[1]+"]";
escape(row,col,swamp,escapePath);
}
哪條線是41?什麼是整個錯誤信息?你沒有收到「一些」錯誤信息。 – nhgrif
escape(row,col,swamp,escapePath); – user2856817
好吧,我看到遞歸阻止您看到錯誤消息。你有沒有試過設置一箇中斷點?我會設置一個斷點,並添加一個輸出行。 – nhgrif