2014-03-31 103 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