繁体   English   中英

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

[英]Why isn't my function being ran in my program (java)?

该程序应该递归解决迷宫。 readMazeFile将文件的内容读取到数组中,然后solveMaze函数使用该数组来求解迷宫。 但是在我的主要功能中,似乎(maze!= null)似乎没有运行。 我包括了它以消除空指针异常。 迷宫=空吗? 我不这么认为,但IDK。 感谢您的帮助。

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;
    }
}

readMazeFilemaze变量readMazeFile了您在条件条件中使用的静态变量。

要么:

  • 分配结果readMazeFile
  • 不要在readMazeFile声明新的maze变量(删除char类型声明readMazeFile )。 然后将其返回就不必要了。

您必须将函数调用的结果存储在变量迷宫的main的第一行中。 由于尚未执行此操作,因此迷宫当然为空。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM