0

When I try to compile this code I get an error saying else without if. I believe that I have all of the braces in the right place. I also get other errors to that are in the picture that I have attached but I just believe they are there because of the can't use else without if error.

Problem code:

public static boolean goNorth(){
        boolean success;
        if(maze[currCol]currRow - 1] == CLEAR){
            maze[currCol][startRow -1] = PATH;
            currRow--;
            success = goNorth();
                if(!success){
                success = goWest();
                    if(!success){
                    success = goEast();
                        if(!success){
                            maze[currCol][currRow] = VISITED;
                            currRow++;
                            }
                        }
                    }
                    return success;
                } else {
                    return false;
            }
        }

    public static boolean goWest(){
        boolean success;
        if(maze[currCol - 1]currRow] == CLEAR){
            maze[currCol - 1][startRow] = PATH;
            currRow--;
            success = goWest();
                if(!success){
                success = goSouth();
                    if(!success){
                    success = goNorth();
                        if(!success){
                            maze[currCol][currRow] = VISITED;
                            currCol++;
                            }
                        }
                    }
                    return success;
                } else {
                    return false;
            }
        }

        public static boolean goEast(){
        boolean success;
        if(maze[currCol + 1]currRow] == CLEAR){
            maze[currCol + 1][startRow] = PATH;
            currRow--;
            success = goEast();
                if(!success){
                success = goNorth();
                    if(!success){
                    success = goSouth();
                        if(!success){
                            maze[currCol][currRow] = VISITED;
                            currCol--;
                            }
                        }
                    }
                    return success;
                } else {
                    return false;
            }
        }

        public static boolean goSouth(){
        boolean success;
        if(maze[currCol]currRow + 1] == CLEAR){
            maze[currCol][startRow + 1] = PATH;
            currRow--;
            success = goSouth();
                if(!success){
                success = goEast();
                    if(!success){
                    success = goWest();
                        if(!success){
                            maze[currCol][currRow] = VISITED;
                            currRow--;
                            }
                        }
                    }
                    return success;
                } else {
                    return false;
            }
        }

Error:

screen shot

4

2 回答 2

3
    if(maze[currCol + 1]currRow] == CLEAR){

那是无效的;您在currRow一些地方缺少左括号。

此外,围绕这一点进行了大量重构。

于 2013-04-25T13:47:56.790 回答
2

阅读第一个错误。在你理解第一个错误之前,永远不要在第一个错误之后阅读任何错误。

第一个错误是告诉您这一行有语法错误:

if(maze[currCol]currRow - 1] == CLEAR){

(然后您将其复制粘贴四次)。它缺少一个[before currRow

于 2013-04-25T13:48:17.370 回答