问题:即使我在Box.java中有一个带有三个参数的构造函数,我也会收到以下错误。
BoxTest.java:19: error: constructor Box in class Box cannot be applied to given types;
Box box1 = new Box(length, width, height);
^
required: no arguments
found: double,double,double
reason: actual and formal argument lists differ in length
分配:使用以下内容创建Box类:
使用以下内容创建BoxTest程序。
BoxTest.java
import java.util.Scanner;
public class BoxTest
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter length: ");
double length = input.nextDouble();
System.out.print("Enter width: ");
double width = input.nextDouble();
System.out.print("Enter heigth: ");
double height = input.nextDouble();
Box[] boxes = new Box[4];
Box box1 = new Box(length, width, height);
boxes[0] = box1;
Box box2 = new Box();
boxes[1] = box2;
Box box3 = new Box(2.0, 3.5, 5.75);
boxes[2] = box3;
Box box4 = new Box(9.0, 12.0, 15.0);
boxes[3] = box4;
System.out.println(displayBox(box1));
}
private static String displayBox(Box box)
{
System.out.print(box.toString());
}
}
Box.java
public class Box
{
// Instance variables
private double length;
private double width;
private double height;
// ********************* DEFAULT CONSTRUCTOR *******
public Box()
{
this(1.0, 1.0, 1.0);
}
// ********************* CONSTRUCTOR *******
public Box(double length, double width, double height)
{
if(length > 0)
{
this.length = length;
}
else
{
this.length = 1.0;
}
if(width > 0)
{
this.width = width;
}
else
{
this.width = 1.0;
}
if(height > 0)
{
this.height = height;
}
else
{
this.height = 1.0;
}
}
public double getLength()
{
return length;
}
public double getWidth()
{
return width;
}
public double getHeight()
{
return height;
}
public void setLength(double length)
{
if(length > 0)
{
this.length = length;
}
}
public void setWidth(double width)
{
if(width > 0)
{
this.width = width;
}
}
public void setHeight(double height)
{
if(height > 0)
{
this.height = height;
}
}
public String toString()
{
return String.format("%s = %.2f%n%s = %.2f%n%s = %.2f%n", "Length", length, "Width", width, "Height", height);
}
public double calculateArea(double length, double width, double height)
{
double area = 2 * (length * width) + 2 * (height * width) + 2 * (length * height);
return area;
}
public double calculateVolume(double length, double width, double height)
{
double volume = length * width * height;
return volume;
}
}
错误:
BoxTest.java:19: error: constructor Box in class Box cannot be applied to given types;
Box box1 = new Box(length, width, height);
^
required: no arguments
found: double,double,double
reason: actual and formal argument lists differ in length
BoxTest.java:25: error: constructor Box in class Box cannot be applied to given types;
Box box3 = new Box(2.0, 3.5, 5.75);
^
required: no arguments
found: double,double,double
reason: actual and formal argument lists differ in length
BoxTest.java:28: error: constructor Box in class Box cannot be applied to given types;
Box box4 = new Box(9.0, 12.0, 15.0);
^
required: no arguments
found: double,double,double
reason: actual and formal argument lists differ in length
^
3 errors
答案 0 :(得分:0)
这有效......
import java.util.Scanner;
public class BoxTest
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter length: ");
double length = input.nextDouble();
System.out.print("Enter width: ");
double width = input.nextDouble();
System.out.print("Enter heigth: ");
double height = input.nextDouble();
Box[] boxes = new Box[4];
Box box1 = new Box(length, width, height);
boxes[0] = box1;
Box box2 = new Box();
boxes[1] = box2;
Box box3 = new Box(2.0, 3.5, 5.75);
boxes[2] = box3;
Box box4 = new Box(9.0, 12.0, 15.0);
boxes[3] = box4;
System.out.println(box1);
}
}
确保自上次更改后重新编译Box.java。而且不需要“displayBox”方法,因为box1有一个toString。