2

所以我正在尝试创建一个程序,当您通过命令行输入 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方面的知识非常有限。

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

4

4 回答 4

1

您正在TestRect通过调用类的构造函数来创建一个新实例。一个很好的教程在这里

 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]));
于 2013-08-15T08:32:45.423 回答
0
  • 您没有将 4 个字符串作为参数的构造函数:
new TestRect((args[0]), (args[1]), (args[2]), (args[3]))
  • area 和 perimeter 是 TestRect 的实例字段。它们只能通过静态上下文中的 TestRect 对象访问。
test.getArea()
test.getPerimeter
于 2013-08-15T08:34:42.153 回答
0

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

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

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

你还需要像这样引用area和:perimeter

test.getArea();
test.getPrimeter();
于 2013-08-15T08:35:04.107 回答
0

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

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

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;
    }
}
于 2013-08-15T08:45:28.830 回答