java - 为什么我的函数没有在我的程序(java)中运行?

标签 java function maze

该程序应该递归地解决迷宫问题。 readMazeFile 将文件的内容读取到数组中,然后solveMaze 函数使用该数组来解决迷宫问题。但在我的主函数中, if (maze != null) 似乎没有运行。我添加它是为了消除空指针异常。迷宫=空吗?我不这么认为,但我不知道。感谢您提前提供的帮助。

public class solving {
    static char maze[][];
    static int startingrow;
    static int startingcol;

    public static void main(String[] args) throws FileNotFoundException {
        readMazeFile("maze0.txt");
        if (maze != null) {
            System.out.print(maze[1][1]);

            if (solveMaze(startingrow, startingcol))
                System.out.print("Solved!");
            else
                System.out.print("There is no solution to this maze.");
        }
    }

    static boolean solveMaze(int row, int col) {
        // name each movement to make coding easier to understand with the recursion.
        char right = maze[row][col + 1];
        char left = maze[row][col - 1];
        char up = maze[row - 1][col];
        char down = maze[row + 1][col];
        char markSpot = 'M';
        char unmarkSpot = ' ';

        // Base case is at the end of the maze
        if (right == 'E' || left == 'E' || up == 'E' || down == 'E') {
            return true;
        }

        // What to do if there is an empty space when it moves
        if (right == ' ') {
            right = markSpot;
            if (solveMaze(row, col + 1)) {
                return true;
            } else {
                right = unmarkSpot;
            }
        }

        if (down == ' ') {
            down = markSpot;
            if (solveMaze(row + 1, col)) {
                return true;
            } else {
                up = unmarkSpot;
            }
        }

        if (left == ' ') {
            left = markSpot;
            if (solveMaze(row, col - 1)) {
                return true;
            } else {
                left = unmarkSpot;
            }
        }

        if (up == ' ') {
            up = markSpot;
            if (solveMaze(row - 1, col)) {
                return true;
            } else {
                up = unmarkSpot;
            }
        }
        return false;
    }

    static char[][] readMazeFile(String mazeFile) throws FileNotFoundException {
        Scanner input = new Scanner(new File(mazeFile));

        // Find the height and width
        int height = input.nextInt();
        int width = input.nextInt();
        int finalHeight = (2 * height) + 1;
        int finalWidth = (2 * width) + 1;

        // Create the array and put data from the file in it
        char maze[][] = new char[finalHeight][finalWidth];
        input.nextLine();

        for (int row = 0; row < finalHeight; row++) {
            String fileLine = input.nextLine();
            for (int col = 0; col < finalWidth; col++) {
                char nextChar = fileLine.charAt(col);
                maze[row][col] = nextChar;
            }
        }

        // Find the starting point
        for (int r = 0; r < finalHeight; r++) {
            for (int c = 0; c < finalWidth; c++) {
                if (maze[r][c] == 'S') {
                    int startingrow = r;
                    int startingcol = c;
                    //System.out.print(startingrow);
                    //System.out.print(startingcol);
                }
            }
        }

        return maze;
    }
}

最佳答案

maze变量 readMazeFile隐藏您在条件中使用的静态变量。

或者:

  • 分配 readMazeFile 的结果.
  • 不要声明新的 maze变量 readMazeFile (删除 char 类型声明符)。这样就没有必要归还它了。

关于java - 为什么我的函数没有在我的程序(java)中运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35247633/

相关文章:

java - 如果我事先不知道它的对象模型,我该如何解析一个 json 字符串?

c - 循环函数不符合我的预期。 C

java - 使用 2D 数组和堆栈在 java 中构造迷宫

c++ - 初学者在 2D 网格上与 Lee 算法作斗争

java - 加入具有不同最后定界符的字符串

c++ - 计算多边形的最小面积矩形

Java 正则表达式能够单独处理嵌套匹配

python - 如何在python中超时功能,超时不到一秒

python - 允许导入的 python 函数调用其他导入的 python 函数

java - 不知何故,在递归过程中,对象在 list.add(object) 之后发生了变异。解释?