2014-03-31 99 views
-2

該程序不起作用,但同時它沒有錯誤通知...每當我輸入一個起始點時,起點都會出現,但沒有路徑到達結束點或路徑通過。該程序有一個7 * 11的迷宮,「B」提出了障礙物和一個固定的結束點,其呈現爲「X」,程序會做什麼,您可以輸入一個座標,使其成爲您的起點。然後程序會從你的出發點到最後找到一種方法(路徑也將顯示爲「O」)。我試圖在這個過程中使用遞歸,但它不起作用,我不知道爲什麼。請幫幫我。Java中的遞歸迷宮

import java.io.*; 
public class Maze { 
private static final Maze[][] String = null; 
String[][] Maze=new String[7][11]; 
int x,y; 
public static void main(String[] args) throws IOException{ 
    String name; 
    int k=0,x1,y1; 
    Maze M=new Maze(); 
    System.out.println("Hello there, would you like to provide your name, please?"); 
    name=M.Name(); 
    M.Maze=M.Set(M.Maze); 
    M.Print(M.Maze); 
    while (k==0){ 
     System.out.println(name+", now please enter coordinate of the starting point"); 
     System.out.println("The left top point would be (0,0)"); 
     System.out.println("Now, please enter the x value:"); 
     M.x=M.Input(); 
     System.out.println("And then, please enter the y value"); 
     M.y=M.Input(); 
     if (M.Maze[M.y][M.x]==" "){ 
      M.Maze[M.y][M.x]="$"; 
      k=1; 
     } 
     else 
      System.out.println("Sorry, you cannot put your starting point there, please try again"); 
    } 
    M.Process(M.x,M.y); 
    M.Print(M.Maze); 
    System.out.println("$ is the Starting Point"); 
    System.out.println("X is the Ending Point"); 
    System.out.println("O is the Path"); 
} 
public static String Name() throws IOException{ 
    String name; 
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 
    name=br.readLine(); 
    return name; 
} 
public static int Input()throws IOException{ 
    int i; 
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 
    i=Integer.parseInt(br.readLine()); 
    return i; 
} 
public static String[][] Set(String Maze[][]){ 
    for(int i=0;i<7;i++){ 
     for(int j=0;j<11;j++){ 
      Maze[i][j]=("B"); 
     } 
    } 
    Maze[1][1]=Maze[2][1]=Maze[3][1]=Maze[4][1]=Maze[5][1]=Maze[1][2]=Maze[1][3]=Maze[2][3]=Maze[3][3]=Maze[5][3]=Maze[3][4]=Maze[5][4]=Maze[1][5]=Maze[2][5]=Maze[3][5]=Maze[5][5]=Maze[3][6]=Maze[4][6]=Maze[5][6]=Maze[1][7]=Maze[2][7]=Maze[2][7]=Maze[3][7]=Maze[5][7]=" "; 
    Maze[6][7]="X"; 
    Maze[1][8]=Maze[3][8]=Maze[1][9]=Maze[3][9]=Maze[4][9]=Maze[5][9]=" "; 
    return Maze; 
} 
public static boolean Process(int x1, int y1){ 
    Maze M=new Maze(); 
    if (Move(M.Maze,x1,y1)){ 
     if (End(x1,y1)) 
      return true; 
    } 
    else{ 
     M.Maze[y1][x1]="1"; 
     if (Process(x1-1,y1)){ 
      M.Maze[y1][x1]="O"; 
      return true; 
     } 
     else if(Process(x1+1,y1)){ 
      M.Maze[y1][x1]="O"; 
      return true; 
     } 
     else if (Process(x1,y1-1)){ 
      M.Maze[y1][x1]="O"; 
      return true; 
     } 
     else if (Process(x1,y1+1)){ 
      M.Maze[y1][x1]="O"; 
      return true; 
     } 
    } 
    return false; 
} 
public static boolean Move(String Maze[][],int x1, int y1){ 
    if (x1<0||y1<0||x1>6||y1>10) 
     return false; 
    if ((Maze[y1][x1]=="B")||Maze[y1][x1]=="1") 
     return false; 
    return true; 
} 
public static boolean End(int x1, int y1){ 
    if ((y1==7)&&(x1==5)) 
     return true; 
    return false; 
} 
public static void Print(String Maze[][]){ 
    for(int i=0;i<7;i++){ 
     for(int j=0;j<11;j++){ 
      System.out.print(Maze[i][j]); 
      System.out.print(" "); 
     } 
     System.out.println(" "); 
    } 
} 
} 
+4

你能否描述1)這個程序應該做什麼,2)*你怎麼認爲它做它應該做的事情? –

+1

你是通過調試器來學習的嗎? –

+1

請不要將'Maze'對象'String'和'String'對象'Maze'的數組調用。事實上,如果您爲變量名使用小寫字母,最好將它們與類名區分開來。 –

回答

0

我不知道,你想用這條線做什麼,因爲它是用來無處:

private static final Maze[][] String = null; 

我發現在你的代碼的幾個問題:

  1. 每當您的代碼輸入Process-方法時,Maze的新實例將使用Maze M=new Maze();創建。新實例不知道在之前的步驟中所做的額外標記(如1),因此它將在兩個相鄰字段之間來回切換,直到出現StackOverflowError。
  2. 此外,新的Maze實例未初始化爲labytinth,因爲您沒有對其調用Set。您應該將Set-方法的代碼放在Maze的構造函數中,這樣就不會發生這種錯誤。
  3. 但是你的程序甚至不會到此爲止。如果您輸入Process-方法是開始字段的cooridinates,它會想知道:「我可以移動到指定的字段嗎?如果是,它是否結束?」。答案是「我可以搬到那裏嗎?」顯然是的,因爲你確定起始字段是清楚的。答案是「這到底是什麼?」沒有。 Process返回true並停止。

    - >你應該怎麼做:將遞歸調用Process放入if中,而不是其他的。你不需要別的東西。

  4. 爲防止項目1中的StackOverflowError發生(現在確實),將Maze的實例傳遞給Process-方法。請記住,每次撥打電話Process時都要這樣做,並且要小心它始終是相同的實例。刪除在Process中產生新的Maze實例的行。

  5. 現在你沒有錯誤,但它仍然不起作用。這是因爲您混合了Move-方法中的x和y最大索引。你聲明這樣的數組:String[][] Maze=new String[7][11];。這意味着,最大的x指數爲10,最大y指數爲6。在Move你檢查這樣的界限:if (x1<0||y1<0||x1>6||y1>10)

  6. 類似的是爲End - 方法真。如果你計算迷宮中的'X',那麼它就是左邊的第8位和頂部的第6位。與此對應的6 Y索引和7.你做(y1==7)&&(x1==5)

現在,它的工作原理X-指數。 1是程序嘗試的路徑。

我認爲你已經注意到你不能從靜態方法中調用非靜態方法,比如主方法。如果您有一個對象的實例,例如代碼中的M,則可以使用M.<methodName>()對其調用非靜態方法。

我強烈建議你更密切地研究面向對象的設計,因爲看起來你不明白它。

如果你遵守java的命名約定 - 只有類和類以大寫字母開頭,其他所有內容都以小寫字母(變量,方法等)開頭,那麼它會很好。它讓那些習慣了它的人閱讀起來更容易,這些人幾乎都是每個人,都是在課堂或書本上教過的。

+0

謝謝!我剛剛看到你的回覆,我的朋友幫我解決了我的問題。但還是非常感謝你! – user3479981