新手求找个错误
import javax.swing.JOptionPane;public class FindWay extends MakeLabyrinth{
public int [][] labyr = new int[8][8];
public String put = new String();
public static void main(String[] args) {
//创建一个迷宫
MakeLabyrinth laby = new MakeLabyrinth();
laby.make();
JOptionPane.showMessageDialog(null, " 迷宫为: \n" + laby.output);
FindWay findLa = new FindWay();
for(int a = 0;a < 8;a++)
for(int b = 0;b < 8;b++)
findLa.labyr[a][b] = laby.labyrinth[a][b];
int i = 1;
int j = 1;
if(findLa.find(i , j) == 1) {
for(int a = 0;a < 8;a++){
for(int b = 0;b < 8;b++) {
findLa.put += findLa.labyr[a][b];
findLa.put += " ";
}
findLa.put += "\n";
}
JOptionPane.showMessageDialog(null, " 迷宫为: \n" + findLa.put);
}
else {
}
}
public int find(int i ,int j) {
if(i == 6 && j == 6) {
return 1;
}
else{
this.labyr[i][j] = 2;
if(this.find(i + 1 , j) == 0 || this.find(i - 1 , j) == 0 || this.find(i , j + 1) == 0 ||this.find(i , j - 1) == 0) {
return 1;
}
else {
this.labyr[i][j] = 0;
return 0;
}
}
}
}
class MakeLabyrinth {
public int [][] labyrinth = new int[8][8];
String output = new String();
//随机一个迷宫
//1代表墙,0代表通路。最后找到的路用2表示。边界为1;
public void make() {
for(int i = 0 ; i < 8 ;i++) {
for(int j = 0; j < 8;j++) {
int number = (int)(Math.random() * 11);
if(i == 0 || i == 7 || j == 0 || j ==7) {
this.labyrinth[i][j] = 1;
}
else if(number > 7) { //墙少一点
this.labyrinth[i][j] = 1;
}
else {
this.labyrinth[i][j] = 0;
}
output += this.labyrinth[i][j] + " ";
}
output += "\n";
}
}