当前位置: 代码迷 >> Java相关 >> 新手求找个错误
  详细解决方案

新手求找个错误

热度:224   发布时间:2013-04-04 10:50:57.0
新手求找个错误
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";
        }   
    }
搜索更多相关的解决方案: static  void  public  import  null  

----------------解决方案--------------------------------------------------------
请问你是要找个什么错误啊??
----------------解决方案--------------------------------------------------------
回复 楼主 叼着奶嘴
解决了,谢谢,递归的条件不对
----------------解决方案--------------------------------------------------------
  相关解决方案