| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 770 人关注过本帖
标题:新手求找个错误
取消只看楼主 加入收藏
叼着奶嘴
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2013-3-21
结帖率:66.67%
收藏
 问题点数:0 回复次数: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 
2013-04-04 10:51
快速回复:新手求找个错误
数据加载中...
 
   



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

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