请在对象类中返回null

时间:2016-03-14 15:01:53

标签: java

我正在尝试获取一个物体(空间)并检查它是否与障碍物重叠。如果是的话,我想返回一个null,如果没有,则返回空间对象。但我不知道该怎么做。我是否在for循环之外的程序末尾添加了if语句?

public Object checkob(Obstacle obstacle, Space space, int numofobst) {
  int object = space.getX();

  System.out.println("space location is " + object + " and the space is " + space.getName());
  for (int ob = 0; ob < numofobst; ob++) {
    if (object == obstacle.getX()) {
      System.out.println("Space " + space.getName() + " is not able to be choosen");
      return null;
    }
    System.out.println("Space " + space.getName() + " is able to be choosen");
    return space;
  }
}

1 个答案:

答案 0 :(得分:1)

我认为你需要将noexcept移到你的循环之外

return space