有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java读取迷宫文件并打印

这个程序是关于迷宫递归的,因为我是Java新手,所以我很难找到如何读取迷宫文件并打印已解决的迷宫。谁能解释一下如何从main方法调用print方法吗?提前谢谢

public class Maze {

    private static char[][] maze;
    private static int rows = 0;
    private static int columns = 0;


    public Maze(char[][] mazeIn) {
        maze = mazeIn;
    }

    private static boolean valid (int r, int c) {

          boolean result = false;

          // check if cell is in the bounds of the matrix
          if (r >= 0 && r < maze.length &&
              c >= 0 && c < maze[0].length)

             //  check if cell is not blocked and not previously tried
             if (maze[r][c] == '1')
                result = true;

          return result;

       }  // method valid

    public static boolean solve (int r, int c) {

          boolean done = false;

          if (valid (r, c)) {

             maze[r][c] = '7';  // cell has been tried

             if (r == maze.length-1 && c == maze[0].length-1)
                done = true;  // maze is solved
             else {
                done = solve (r+1, c);  // down
                if (!done)
                   done = solve (r, c+1);  // right
                if (!done)
                   done = solve (r-1, c);  // up
                if (!done)
                   done = solve (r, c-1);  // left
             }
             if (done)  // part of the final path
                maze[r][c] = '8';
          }

          return done;

       }  // method solve

    public static void print () {

          System.out.println();

          for (int r=0; r < maze.length; r++) {
             for (int c=0; c < maze[r].length; c++)
                System.out.print (maze[r][c]);
             System.out.println();
          }

          System.out.println();

       }  // method print_maze


    public static void main(String[] args) throws IOException  {

      String fileName = "Maze.txt";

      try {
            String readline;

            FileReader fileReader = 
                    new FileReader(fileName);

                BufferedReader br = 
                    new BufferedReader(fileReader);

                int line = 0;
                while((readline = br.readLine()) != null) {
                System.out.println(readline); //loads the maze

                char[] charArr = readline.toCharArray();
                maze[line] = charArr;  // error here

                line++;
                }

        br.close();         
    }

    catch(FileNotFoundException ex) {
        System.out.println(
            "Unable to open file '" + 
            fileName + "'");                
    }
    catch(IOException ex) {
        System.out.println(
            "Error reading file '" 
            + fileName + "'");                  

        }
    }
} 

迷宫。txt文件如下所示

 000100000000000  
 000100001000010  
 000111111111000   
 000100000001000  
 000111110001000  
 000000010001000  
 000011110001000  
 000010010001010  
 000010010000000  
 000010000000000  
 000011111110000  
 000000000010000  
 000000000010000  
 000001000011110  
 000000000010000  

共 (1) 个答案

  1. # 1 楼答案

    有几件事我会做得不同,但重要的是你没有用任何数据填充字符数组。在读取数据时,需要将其填充到char数组中。像这样的方法会奏效:

     int line = 0;
     while((readline = br.readLine()) != null) {
         System.out.println(readline); //loads the maze
         char[] charArr = readline.toCharArray();
         maze[line] = charArr;
         line++;
     }
    

    此外,迷宫数组从未实际实例化,主方法从未调用“solve()”或“print()”。这些是你可以用主要方法处理的事情

    最后,如果要调用“solve()”,则必须决定是否要实例化迷宫类的实例并调用solve(这将涉及大量代码更改,但这是正确的方法),否则“solve()”也应该是静态方法,这意味着“valid()”也必须是静态的

    注意:你的for循环中有一个错误

    for (int c=0; columns < maze[r].length; c++)
    

    你应该把“列”改成“c”

    另外,在你的求解方法中,你将int赋值给一个字符数组。将7和8放在单引号中,表示它们是字符,而不是整数

     maze[r][c] = '7';  // cell has been tried
    

    这在valid()方法中尤其重要,因为您正在测试迷宫[r][c]==1,但您应该测试迷宫[r][c]==1

    我自己做了所有这些更改,并将其作为输出

     000700000000000  
     000700007000010  
     000777777777000   
     000700000007000  
     000777770007000  
     000000070007000  
     000077770007000  
     000070070007010  
     000070070000000  
     000070000000000  
     000077777770000  
     000000000070000  
     000000000070000  
     000001000077770  
     000000000070000