我寫了一個關於計算一年中下雨的平均值並給我最高和最低金額的程序,但我收到一個錯誤,但我無法修復它。java中RainFall程序中的輸出錯誤
這是我的主要代碼:`
public class Rainfall {
//field
private double [] rain;
public void Rainfall (double [] array) {
rain = new double [array.length] ;
for (int i=0; i < array.length ; i++)
rain [i] = array [i];
}
//Methods
public double getTotal() {
double total = 0;
for (double r : rain)
total +=r;
return total;
}
public double getAverage() {
double a = getTotal()/rain.length;
return a;
}
public double getHighest() {
double highest = rain [0];
for (int i=0 ; i < rain.length; i++) {
if (rain [i] > highest) ;
highest = rain [i];
}
return highest;
}
public double getLowest() {
double lowest = rain [0];
for (int i=0; i <rain.length; i++) {
if (rain [i] < lowest) ;
lowest = rain [i];
}
return lowest;
}
}
` ,這是演示:
`
import java.util.Scanner;
import java.text.DecimalFormat;
public class RainFallDemo {
public static void main (String [] args) throws NullPointerException
{
final int year1 = 12;
double [] rains = new double [year1];
getData (rains) ;
Rainfall rainfallData = new Rainfall();
DecimalFormat values = new DecimalFormat ("#0.00") ;
System.out.println("The total rain is" + values.format(rainfallData.getTotal()));
System.out.println("The average is " + values.format (rainfallData.getAverage()));
System.out.println("The highest rain is " + values.format (rainfallData.getHighest()));
System.out.println("The lowest is " + values.format (rainfallData.getLowest()));
}
public static void getData (double [] array) {
Scanner keyboard = new Scanner (System.in) ;
for (int i=0 ; i < array.length; i++)
{
System.out.println ("what was the amont of rain for month " + (i + 1) + "?");
array [i] = keyboard.nextDouble();
while (array[i] < 0)
{
System.out.print (" Invalid input. You entered a negative number. Enter a" +
"value that is greater than zero");
array [i] = keyboard.nextDouble();
}
}
}
}`
我得到了錯誤的最低和錯誤的最高和程序有哪些月份顯示有輸出的最低和最高,但我無法弄清楚代碼!
'public void Rainfall(double [] array)'是**不是**構造函數。構造函數不應該有*任何*返回類型,這包括'void'。 – 2014-11-06 01:53:57
謝謝你,我解決了這個錯誤,但是你能幫助我解決其他問題嗎? @ PM77-1 ////我得到這個輸出:總雨爲561.00 平均爲46.75 最高雨爲99.00 最低爲11.00我在想我應該怎麼寫才能輸出程序說哪個月有最高和最低的雨量 – Taix 2014-11-06 02:08:20