简体   繁体   English

System.out.println()不打印任何内容。

[英]System.out.println() not printing anything.

This code compiles, but doesn't print anything into the terminal. 这段代码可以编译,但是不会在终端上打印任何内容。

So my code is meant to compare the y co-ordinate of the robot with the y co-ordinate of the target. 因此,我的代码旨在将机器人的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;}

  }
}

I guess it is supposed to be: 我猜应该是:

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;}

  }
}

My guess is that you just wrote this function and you aren't calling it anywhere. 我的猜测是您只是编写了此函数,而未在任何地方调用它。

Make a main with: 以以下内容为主:

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);
}

You have a method named isTargetNorth , but there is nothing that invokes it. 您有一个名为isTargetNorth的方法,但是没有任何方法可以调用它。 You need to invoke the method. 您需要调用该方法。 Create a main() method that invokes isTargetNorth . 创建一个调用isTargetNorthmain()方法。 Compile the class, then you can run it from the command line. 编译该类,然后可以从命令行运行它。 It's easier if you're using an IDE, though, since it will probably let you run the class from the IDE once you've created the main method. 但是,如果您使用的是IDE,它会更容易,因为一旦创建了main方法,它可能会让您从IDE中运行类。 Assuming you have a Robot implementation: 假设您有一个Robot实现:

public static void main(String[] args) {
    IRobot robot = new RobotImpl();
    isTargetNorth(robot);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM