【问题标题】:Java program error with tostring带有 tostring 的 Java 程序错误
【发布时间】:2013-08-15 08:28:55
【问题描述】:

所以我正在尝试创建一个程序,当您通过命令行输入 4 个参数时,例如 1 2 3 4。它输出:

java TestRect 1 2 3 4
rectangle = (1.0, 2.0, 3.0, 4.0)
area = 12.0
perimeter = 14.0

这是我目前所拥有的:

public class TestRect {

  private double x;
  private double y;
  private double base;
  private double height;
  private double area;
  private double perimeter;

  public double getPerimeter () {
     perimeter = 2 * (base + height);
     return perimeter;
  }

  public double getArea () {
     area = (base * height);
     return area;
  } 

  @Override
  public String toString() {
  return "("+x+","+y+","+base+","+height+")"; 
  }

  public static void main(String[] args) {

      TestRect test = new TestRect((args[0]), (args[1]), (args[2]), (args[3]));
      System.out.println(test.toString());
      System.out.println("Area = " + area);
  System.out.println("Perimeter = " + perimeter);
   }

}

当我运行程序时,我收到一条错误消息:

TestRect.java:27: error: constructor TestRect in class TestRect cannot be applied to       given types;
      TestRect test = new TestRect((args[0]), (args[1]), (args[2]), (args[3]));
                      ^
  required: no arguments
  found: String,String,String,String
  reason: actual and formal argument lists differ in length
TestRect.java:29: error: non-static variable area cannot be referenced from a static   context
      System.out.println("Area = " + area);
                                     ^
TestRect.java:30: error: non-static variable perimeter cannot be referenced from a static context
  System.out.println("Perimeter = " + perimeter);
                                      ^
3 errors

我做错了什么?我的java知识非常有限。

*完全披露:该计划不适用于任何作业或家庭作业。这纯粹是为了我的知识。

【问题讨论】:

  • 我认为编译器错误已经很清楚了。您没有匹配的构造函数来获取您传递的参数。您需要在TestRect 类中声明一个构造函数,该构造函数接受 4 个字符串参数。
  • 你的 TestRect 构造函数在哪里?
  • @ihsankocak arg[0] 不是 Java 中的程序名称,但实际上是第一个参数(与 C 相对)。没有“程序”,而是具有main(String[]) 方法的“主类”。见docs.oracle.com/javase/tutorial/essential/environment/…

标签: java string class tostring println


【解决方案1】:

您正在通过调用类的构造函数来创建TestRect 的新实例。好tutorial is here

 TestRect test = new TestRect((args[0]), (args[1]), (args[2]), (args[3]));

你需要声明构造函数:

public class TestRect {
    // your fields here

     public TestRect(double x, double y, double base, double height) {
         this.x = x;
         this.y = y;
         this.base = base;
         this.height = height;             
     }

    // the rest of your class

然后你就可以调用它了:

   TestRect test = new TestRect(Double.parseDouble(args[0]), Double.parseDouble(args[1]), Double.parseDouble(args[2]), Double.parseDouble(args[3]));

【讨论】:

  • 使用String number = "12.34"; double value = Double.parseDouble(number); 创建双打。但不要在 TestRect 类中这样做。
【解决方案2】:

我没有使用任何 cmets,但它应该是不言自明的。如果没有,请评论。

我添加了一种读取双打的方法以避免代码重复。它尝试将字符串转换为双精度并捕获可能发生的异常。

public class TestRect {

    private final double x;
    private final double y;
    private final double base;
    private final double height;

    private double area;
    private double perimeter;

    public TestRect(double x, double y, double base, double height) {
        this.x          = x;
        this.y          = y;
        this.base       = base;
        this.height     = height;
        this.perimeter  = 2 * (base + height);
        this.area       = base * height;
    }

    public double getPerimeter()    { return perimeter; }
    public double getArea()         { return area; }
    @Override
    public String toString()        { return "(" + x + ", " + y + ", " + base + ", " + height + ")"; }

    public static void main(String[] args) {

        double x = 0, y = 0, base = 0, height = 0;

        if (args.length == 4) {
                x       = readDoubleFromString(args[0]);
                y       = readDoubleFromString(args[1]);
                base    = readDoubleFromString(args[2]);
                height  = readDoubleFromString(args[3]);
        }

        TestRect test = new TestRect(x, y, base, height);

        System.out.println(test.toString());
        System.out.println("Area = " + test.getArea());
        System.out.println("Perimeter = " + test.getPerimeter());
    }

    private static double readDoubleFromString(String d) {
        double n = 0;
        try {
            n = Double.parseDouble(d);
        } catch (NumberFormatException e) {
            System.out.println(d + " is not a valid double. 0.0 is used instead!");
        }
        return n;
    }
}

【讨论】:

  • 感谢 mike 解决了我的问题。你的回答让我很容易看出我的错误。
  • Np。小提示:我个人会将main 方法和readDoubleFromString 方法移到另一个类(例如MainAppLauncherTest 等)。您应该将类​​与您对它们所做的事情隔离开来。但是,如果您只想做一个简短的示例,那就是要走的路!
【解决方案3】:
  • 您没有使用 4 个字符串作为参数的构造函数:
new TestRect((args[0]), (args[1]), (args[2]), (args[3]))
  • area 和 perimeter 是 TestRect 的实例字段。它们只能通过静态上下文中的 TestRect 对象访问。
test.getArea()
test.getPerimeter

【讨论】:

    【解决方案4】:

    就像其他 cmets 指出的那样,您需要定义一个接受相关参数的构造函数。默认情况下,编译器只插入一个空的、无参数的构造函数。

    这应该可以完成这项工作:

    public TestRect(double x, double y, double base, double height){
        this.x = x;
        this.y = y;
        this.base = base;
        this.height = height;
    }
    

    您还需要像这样引用areaperimeter

    test.getArea();
    test.getPrimeter();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-29
      • 2020-06-06
      • 1970-01-01
      • 2011-11-13
      相关资源
      最近更新 更多