java - 错误: Constructor cannot be applied to given types

标签 java arrays constructor

所以我正在为这个降雨作业编写一个类,我想我可以让它工作,但我对程序的最后部分“Rainfall rain = new Rainfall();”有问题。

我知道到目前为止,我的代码中可能存在一些逻辑错误,但我专注于尝试至少将其打印出来,以便我可以解决这些问题。谢谢!

/**
Rainfall.java calculated the total annual and average
monthly rainfall from the array. This program also returns
the month with the most rainfall and the month with the least rainfall.
*/


public class Rainfall 
{

  //set integer month to 12
  private int month = 12;

  private double[] months;

/**
    Constructor
    @param scoreArray An array of test scores
*/
  public Rainfall(double[] rainfallArray)
  {
     months = rainfallArray;
  }
//Get total annual rainfall
  public double getTotal() 
  {
     double total = 0;

    //for loop to go through the entire array to calculate the total amount of rainfall
     for (int i = 0; i < month; i++) 
     {
        total = total + months[i];
     }

     return total;
  }

//Calculate average monthly rainfall
  public double getAverage()
  {
     double total = 0; //To hold the current total amount of rainfall
     double average; //To hold the average amount of rainfall

     for (int i = 0; i < month; i++) 
     {
        total = total + months[i];
     }

    //Calculate average rainfall 
     average = total / (months.length + 1);

     return average;
  }
//Get month with the most rain
  public double getMost()
  {
     double most; //To hold the most amount of rainfall

    //set the first month in the array
     most = months[0];

     int m=0;


     for (int i = 0; i < month; i++) 
     {
        if (months[i] < most) 
        {
           m=i;
        }
     }
     return most;
  }

//Get month with the least rain
  public double getLeast()
  {
     double least; //To hold the least amount of rainfall
     int m=0;

    //set the first month in the array
     least = months[0];


     for (int i = 0; i < month; i++) 
     {
        if (months[i] < least) 
        {
           m = i;
        }
     }

     return least;
  }

  public static void main(String[ ] args) 
  {

     Rainfall rain = new Rainfall();
     rain.setMonths(); 

    //Display results of the Total, Avg, and Most and Least calculations of rainfall
     System.out.println("The total rainfall for the year: " + rain.getTotal());
     System.out.println("The average rainfall for the year: " + rain.getAverage());
     System.out.println("The month with most rain: " + rain.getMost());
     System.out.println("The month with least rain: " + rain.getLeast());
  }


}

最佳答案

更改构造函数以使用 varargs参数:

public Rainfall(double... rainfallArray) {
    months = rainfallArray; // a varargs comes in as an array, ie double[]
}

然后,当您构造它时,您可以传递任意数量的 double (包括无)。

即,所有这些都可以:

Rainfall r1 = new Rainfall();
Rainfall r2 = new Rainfall(4);
Rainfall r3 = new Rainfall(4, 5, 6, 15);

注意,在不传递任何参数的情况下,即new Rainfall(),仍然会传入一个数组,但它的长度为零。


如果您想断言传入了特定的月份数,请将其添加到您的构造函数中:

if (rainfallarray.length != 12)
    throw new IllegalArgumentException(
        "Expecting 12 months of data, but only got " + rainfallarray.length);

关于java - 错误: Constructor cannot be applied to given types,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16804350/

相关文章:

JavaScript for 内循环

javascript - 数组自动更改值

c++ - 构造函数 Qt C++ 中的枚举

Java Wicket - 在 AJAX 之前调用 javascript ( JQuery )

php - 将数组附加到多维数组,保留键

c++ - 在哪些情况下调用 C++ 复制构造函数?

c++ - 具有一个默认参数的构造函数

java - 由于连接重置,Google App Engine 生成 Cloud Endpoint 失败

java - java.time.Instant 中纳秒是如何计算的?

java - 在 JUNIT 中按数据库查询执行组