如何在java中的一个方法中有一个return语句?

时间:2018-03-30 22:10:02

标签: java methods

我知道如何在一个简单的情况下在方法中返回一个,但是当一个方法从另一个类返回一个对象时,我不知道如何给一个变量来存储返回值。具体代码如下所示:

public Path findPath(Maze maze) {
    Path path = new Path();
    MazePosition initialPos = new MazePosition(0, 0, null);
    Stack<MazePosition> posForExplore = new Stack<MazePosition>();
    MazePosition pos = initialPos;
    MazePosition next;

    posForExplore.push(pos);
    while (!posForExplore.empty()) {
        pos = posForExplore.pop();
        switch (maze.getPosStatus(pos)) {
        case GOAL:
            MazePosition p = pos;
            while (p!=null) {
                path.insertFirst(p.getCoords()[0], p.getCoords()[1]);
                p =p.getFrom();
            }
            return path;
        case VISITED:
            break;
        case OBSTACLE:
            break;
        case OPEN:
            maze.setPosStatus(pos, MazeStatus.VISITED);
            for (Movement mov: DIRS_TO_EXPLORE) {
                next = maze.getNeighbour(pos, mov);
                if (next!=null&&(maze.getPosStatus(next)==MazeStatus.OPEN||maze.getPosStatus(next)==MazeStatus.GOAL)) {
                    posForExplore.push(next);
                }
            }
            break;
        }
    }
    return null;
}

我有一个返回路径并返回null,所以我怎么才能有一个return语句。非常感谢你!

3 个答案:

答案 0 :(得分:2)

path声明并初始化为null。在插入任何内容之前检查null并初始化它(打破循环以触发返回)。最后return path。像,

public Path findPath(Maze maze) {
    Path path = null;
    MazePosition initialPos = new MazePosition(0, 0, null);
    Stack<MazePosition> posForExplore = new Stack<MazePosition>();
    MazePosition pos = initialPos;
    MazePosition next;

    posForExplore.push(pos);
    loop: while (!posForExplore.empty()) {
        pos = posForExplore.pop();
        switch (maze.getPosStatus(pos)) {
        case GOAL:
            MazePosition p = pos;
            while (p != null) {
                if (path == null) {
                    path = new Path();
                }
                path.insertFirst(p.getCoords()[0], p.getCoords()[1]);
                p = p.getFrom();
            }
            break loop;
        case VISITED:
            break;
        case OBSTACLE:
            break;
        case OPEN:
            maze.setPosStatus(pos, MazeStatus.VISITED);
            for (Movement mov : DIRS_TO_EXPLORE) {
                next = maze.getNeighbour(pos, mov);
                if (next != null && (maze.getPosStatus(next) == MazeStatus.OPEN
                        || maze.getPosStatus(next) == MazeStatus.GOAL)) {
                    posForExplore.push(next);
                }
            }
            break;
        }
    }
    return path;
}

答案 1 :(得分:0)

以下是您必须采取的步骤

  • 尝试在开头将新的Path实例初始化为null 你的方法例如pathReturn
  • 在每个switch语句中,在break之前,指定pathReturn 到您的结果然后break
  • 在您的方法结束时,只需返回pathReturn而不是null

答案 2 :(得分:0)

要从方法的中间消除return path;,并且最后只有一个return语句,您需要能够退出return path;所在的循环,您需要指定返回值不应为null

Path returnPath = null;
LOOP: while (!posForExplore.empty()) {
    ...
    switch (maze.getPosStatus(pos)) {
        case GOAL:
            ...
            returnPath = path;
            break LOOP;
        ...
    }
}
return returnPath;