所以我正在尝试创建一个程序,当您通过命令行输入 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方面的知识非常有限。
*完全披露:该计划不适用于任何作业或家庭作业。这纯粹是为了我的知识。