我收到错误“实际和正式的参数列表长度不同”,即使我有一个3个双参数的构造函数

时间:2017-12-03 18:52:15

标签: java arrays

问题:即使我在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类:

  • 实例变量长度,宽度和高度
  • 无参数Box构造函数 - 使用1.0作为宽度,长度和高度的默认值。
  • 具有三个参数的Box构造函数。实例变量可以没有零值或负值。如果参数无效,请使用1.0作为宽度,长度或高度的默认值。
  • 为每个实例变量获取并设置(访问器和更改器)方法。实例变量可以没有零值或负值,因此请务必验证set方法中的参数。如果参数无效(零或负),请勿更新实例变量。不要使用异常处理。
  • 一个toString方法,返回一个带有长度,宽度和高度的字符串,清楚地标记。将每个值格式化为小数点后的两个位置。
  • 计算并返回框的表面区域(全部6个边)的calculateArea方法
  • 计算并返回框音量的calculateVolume方法

使用以下内容创建BoxTest程序。

  • 一个名为box的数组,其大小(长度)为4
  • 4个Box对象 - 将每个Box放在数组中。必须使用用户输入创建一个Box对象的长度,宽度和高度。必须使用无参数构造函数创建一个Box对象。应使用文字值(2,3.5,5.75等)创建其他Box。
  • 使用增强的for循环显示每个Box对象的长度,宽度,高度,面积和体积,清楚标记。通过调用适当的方法来完成此操作。调用toString方法获取长度,宽度和高度。将区域和音量格式化为小数点后两位。

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

1 个答案:

答案 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。