这个程序不起作用,但同时它没有错误通知......每次我进入起点时,起点都会出现,但没有到终点的路径,也没有路径。该计划有一个7 * 11的迷宫," B"提出障碍和固定的终点,表示为" X"程序将会做什么,你可以输入一个坐标作为你的起点。然后程序会找到从起点到终点的路径(路径也会显示在" O")。我尝试在这个过程中使用递归,但是它不起作用,我不知道为什么。请帮帮我们。
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(" ");
}
}
}
答案 0 :(得分:0)
我不确定,你想用这条线做什么,因为它无处使用:
private static final Maze[][] String = null;
我在您的代码中发现了几个问题:
Process
- 方法时,都会使用Maze
创建Maze M=new Maze();
的新实例。新实例不知道您在之前的步骤中做出的额外标记(如1),因此它将在两个相邻字段之间来回切换,直到您收到StackOverflowError。Maze
实例未使用labytinth初始化,因为您没有在其上调用Set
。您应该将Set
- 方法的代码放在Maze
的构造函数中,以便不会发生此类错误。但是你的程序甚至没有走到这一步。如果你给你的起始字段的cooridinates提供Process
- 方法,它会想:“我可以移动到指定的字段,如果是,那么结束了吗?”。答案是“我可以搬到那里吗?”显然是的,因为你确保起始字段是清楚的。答案是“这是结束吗?”没有。 Process
返回true并停止。
- &GT;你应该怎么做:将Process
的递归调用放入if,而不是else。你不需要别的。
要防止第1项发生StackOverflowError
(现在就发生),请将Maze
的实例传递给Process
- 方法。每次调用Process
时都要记住这样做,并注意它始终是同一个实例。删除在Maze
中生成新Process
实例的行。
目前您没有错误,但仍然无效。这是因为您在Move
- 方法中混合了x和y最大索引。您将数组声明为:String[][] Maze=new String[7][11];
。这意味着最大x-index为10,最大y-index为6.在Move
中,您可以检查这样的边界:if (x1<0||y1<0||x1>6||y1>10)
End
- 方法也是如此。如果计算迷宫中的“X”,它是从左到右的第8个,从顶部到第6个。这相当于y指数为6,x指数为7.你做了(y1==7)&&(x1==5)
现在它有效。 1是程序尝试的路径。
我想你已经注意到你不能像main-mehtod这样的静态方法调用非静态方法。如果您有一个对象的实例,例如代码中的M
,则可以使用M.<methodName>()
在其上调用非静态方法。
我强烈建议你更仔细地研究面向对象的设计,因为它似乎你不理解它。
如果你坚持使用java命名约定也会很好 - 只有类和大写字母开头,其他一切都以小写字母(变量,方法等)开头。它使那些习惯了它的人更容易阅读,这几乎是每个人,在课堂上或书中教过。