2014-11-06 19 views
0

我寫了一個關於計算一年中下雨的平均值並給我最高和最低金額的程序,但我收到一個錯誤,但我無法修復它。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(); 

      } 
    } 
} 
}` 

我得到了錯誤的最低和錯誤的最高和程序有哪些月份顯示有輸出的最低和最高,但我無法弄清楚代碼!

+1

'public void Rainfall(double [] array)'是**不是**構造函數。構造函數不應該有*任何*返回類型,這包括'void'。 – 2014-11-06 01:53:57

+0

謝謝你,我解決了這個錯誤,但是你能幫助我解決其他問題嗎? @ PM77-1 ////我得到這個輸出:總雨爲561.00 平均爲46.75 最高雨爲99.00 最低爲11.00我在想我應該怎麼寫才能輸出程序說哪個月有最高和最低的雨量 – Taix 2014-11-06 02:08:20

回答

4

刪除if行尾部的分號。如果在true時執行空語句,那會導致if。然後,無論如何,下一行始終執行。

+0

謝謝你的男人,我修正了錯誤,但你能幫助我的另一部分? ////我得到這個輸出:總雨爲561.00 平均爲46.75 最高雨爲99.00 最低爲11.00我在想我應該怎麼寫才能說輸出的程序可以說哪個月有最高和最低的雨量 – Taix 2014-11-06 02:10:13

+0

你可以做幾件事:添加public int getHighestMonth(),它模仿getHighest()的邏輯,但返回我而不是雨[i],或者我的首選方法是添加類成員int highMonth,lowMonth;並將它們設置在getHighest和getLowest中。在這種情況下,我還會增加成員最長,最低的;並將getHighest()更改爲void calculateHighest(); – TubaBen 2014-11-06 02:39:13

+0

本我很感激你的幫助,但我仍然有點混淆。你能爲我寫getHighestMonth()代碼嗎? – Taix 2014-11-06 02:52:34