Java 返回语句

Java 返回语句,java,return,Java,Return,如何使此代码正常工作?我需要返回3个场景中的语句,目前我在字符串robotInfo中遇到一个错误 String generateStatusReport(Robot robot) { String robotStatus; String robotWall; String robotGround; String robotInfo = robotStatus + robotWall + robotGround; if(isRobotDead(robot

如何使此代码正常工作?我需要返回3个场景中的语句,目前我在字符串robotInfo中遇到一个错误

String generateStatusReport(Robot robot) {

    String robotStatus;
    String robotWall;
    String robotGround;
    String robotInfo = robotStatus + robotWall + robotGround;

    if(isRobotDead(robot)) {
        robotStatus = ("The robot is dead.");
    } else {
        robotStatus = ("The robot is alive.");
        if(isRobotFacingWall(robot)) {
            robotWall = ("The robot is facing a wall.");
        } else {
            robotWall = ("The robot is not facing a wall.");
        }

        if(isItemOnGroundAtRobot(robot)) {
            robotGround = ("There is an item here.");
        } else {
            robotGround = ("There is no item here.");
        }
    }
    return robotInfo;
}

您需要初始化字符串以获取robotInfo中的值

String robotStatus;
    String robotWall;
    String robotGround;

我会将连接移动到条件语句之后但返回语句之前:

String generateStatusReport(Robot robot) {

    String robotStatus;
    String robotWall;
    String robotGround;

    if(isRobotDead(robot))
        robotStatus = ("The robot is dead.");
    else {
        robotStatus = ("The robot is alive.");
        if(isRobotFacingWall(robot))
            robotWall = ("The robot is facing a wall.");
        else
            robotWall = ("The robot is not facing a wall.");

        if(isItemOnGroundAtRobot(robot))
            robotGround = ("There is an item here.");
        else
            robotGround = ("There is no item here.");
    }
    String robotInfo = robotStatus + robotWall + robotGround;
    return robotInfo;
}
或者只返回连接:

return robotStatus + robotWall + robotGround;