0

此代码可以编译,但不会将任何内容打印到终端中。

所以我的代码旨在将机器人的 y 坐标与目标的 y 坐标进行比较。

public class Ex12 
{

  private byte isTargetNorth(IRobot robot)
  {

    if (robot.getLocationY() > robot.getTargetLocation().y) 
      {System.out.println("north");
      return 1;}

    else if (robot.getLocationY() == robot.getTargetLocation().y)
      {System.out.println("no");
      return 0;}

    else 
      {System.out.println("south");
      return -1;}

  }
}
4

3 回答 3

0

我的猜测是你只是写了这个函数,你没有在任何地方调用它。

做一个主要的:

public static void main(String[] args) {
    //Create robot instance, assuming a Robot implementation is
    //named Robot and has a default constructor.
    IRobot robot = new Robot();

    //Create instance of example class since your function is not static.
    Ex12 instance = new Ex12();
    instance.isTargetNorth(robot);
}
于 2013-10-27T21:10:13.913 回答
0

您有一个名为 的方法isTargetNorth,但没有任何东西可以调用它。您需要调用该方法。创建一个main()调用isTargetNorth. 编译类,然后你可以从命令行运行它。但是,如果您使用 IDE 会更容易,因为一旦您创建了 main 方法,它可能会让您从 IDE 运行类。假设您有一个 Robot 实现:

public static void main(String[] args) {
    IRobot robot = new RobotImpl();
    isTargetNorth(robot);
}
于 2013-10-27T21:10:18.380 回答
0

我想应该是:

public static void main(String[]args)
{

     IRobot robot = new IRobot().

     Ex12 instance = new Ex12().

     instance.isTargetNorth(robot);


}

   public class Ex12 
{

  public byte isTargetNorth(IRobot robot)
  {

    if (robot.getLocationY() > robot.getTargetLocation().y) 
      {System.out.println("north");
      return 1;}

    else if (robot.getLocationY() == robot.getTargetLocation().y)
      {System.out.println("no");
      return 0;}

    else 
      {System.out.println("south");
      return -1;}

  }
}
于 2013-10-27T21:06:16.217 回答