2013-09-21 150 views
0

如何獲得此代碼的工作?我需要返回3個場景中的語句,目前我在String 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; 
} 

回答

0

您需要初始化字符串robotInfo

獲得價值
String robotStatus; 
    String robotWall; 
    String robotGround; 
1

我會條件後您的級聯移至但return語句之前:

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;