2

所以我正在为这个降雨作业写一堂课,我想我可以让它工作,但我对程序“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());
  }


}
4

3 回答 3

3

更改您的构造函数以使用可变参数:

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

然后当你构造它时,你可以传递任意数量的doublesin(包括没有)。

即,所有这些都可以:

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);
于 2013-05-29T02:08:35.713 回答
1

问题是您的构造函数期望double array被调用rainfallArray并且构造函数不包含任何参数;它是空的。用双数组初始化,它将构造。

像这样:

double [] rain = {1.3, 2.4, 3.5, 6.2}; 
 Rainfall rain = new Rainfall(rain);
... rest of code in main

此外,你for loops应该看起来像:

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

如果您按照以前的方式进行操作,如果您没有一个大小为 12 个元素的数组,您将获得空指针异常。另外,请记住,数组是基于 0 的,因此您可能希望以 1 开始 for 循环,或者month如果您使用现在使用的逻辑,则将变量设置为 11。

于 2013-05-29T02:04:18.167 回答
0

你的 Rainfall 类需要数据,这是你在构造函数中决定的(它必须有一个双精度数组),当你尝试构造一个没有数据的 Rainfall 对象时,你违反了自己的规则,因此出现错误。您必须向它传递一个双精度数组(即使它不包含数据),才能使对象以有效状态存在(根据您的规则定义,构造函数)。

所以new Rainfall()无效。您必须向它传递一个双精度数组。

double[] data = new double[12];
//fill in data, do stuff
Rainfall rain = new Rainfall(data);
于 2013-05-29T02:41:44.220 回答