2013-05-29 155 views
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()); 
    } 


} 
+0

感谢您的帮助,我其实是做了这一切错误的大声笑。我redid它,并确保我问用户输入的月份,而不是尝试访问已有的数字数组。第一次尝试的指示非常非常混乱。 – iamgod

回答

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; 
     } 
    } 

如果你这样做,你有过的方式,你会如果你不得到空指针异常t有一个12个元素的数组。另外,请记住数组是基于0的,因此如果您使用现在使用的逻辑,则需要使用1开始for循环,或将month变量设置为11。

3

更改您的构造函数使用varargs参数:

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

然后,当你建造它,你可以通过在(包括不含)的任何数量的doubles

即,所有的这些将罚款:

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); 
+0

他的构造函数正在寻找一个数组,而不是单个的值。 – BlackHatSamurai

+1

@Blaine可变参数** IS **数组! – Bohemian

+0

好点。抱歉。 – BlackHatSamurai

0

你雨量类需要的数据,这已在构造决定(它必须有一个双精度数组),并且当你尝试构造一个没有数据的Rainfall对象时,就违反了你自己的规则,从而导致错误。您必须为它传递一个双精度数组(即使它不包含数据),以使对象以有效状态存在(按照您的规则,构造函数定义)。因此new Rainfall()无效。您必须将它传递给一个双数组。

double[] data = new double[12]; 
//fill in data, do stuff 
Rainfall rain = new Rainfall(data); 
相关问题