标题:新手求找个错误
取消只看楼主
叼着奶嘴
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2013-3-21
结帖率:66.67%
已结贴  问题点数:10 回复次数:1 
新手求找个错误
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 
2013-04-04 10:50
叼着奶嘴
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2013-3-21
得分:0 
回复 楼主 叼着奶嘴
解决了,谢谢,递归的条件不对
2013-04-04 16:13



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-403021-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.346317 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved