2014-08-29 27 views
-2

我已經編寫了代碼,它將採用每個月的溫度,然後顯示並計算年份的總體,平均,最多和最少降雨量並輸出。如何用實際的月份名稱替換最多和最少的降雨量?寫一個temp。數組

到目前爲止的代碼:

import java.util.Scanner; 
import java.text.DecimalFormat; 

public class Rainfall 
{ 
    public static void main(String[] args) 
    { 
    String [] months={"Janurary","Febuary","March","April","May","June","July","August","September","October","November","December"}; 

    final int MONTHS = 12; 
    double[] rain = new double[MONTHS]; 

    initRain(rain); 
    double total = totalRain(rain); 
    double average = averageRain(rain, total); 
    int most = mostRain(rain); 
    int least = leastRain(rain); 
    // Decimal Format 
    DecimalFormat digit = new DecimalFormat("#.0"); 
    // Output 
    System.out.println("The total rainfall of the year is " + digit.format(total)); 
    System.out.println("The average rainfall of the year is " + digit.format(average)); 
    System.out.println("The month with the highest amount of rain is " + (most + 1)); 
    System.out.println("The month with the lowest amount of rain is " + (least + 1)); 
    } 

    public static void initRain(double[] array) 

    { 
    Scanner keyboard = new Scanner(System.in); 
    for (int x = 0; x < array.length; x++) 
    { 
     System.out.print("Enter Rainfall for month " + (x + 1) + ": "); 
     array[x] = keyboard.nextDouble(); 
    } 
    } 
    public static double totalRain(double[] array) 
    { 
    double total = 0; 
    for (int x = 0; x < 12; x++) 
     total += array[x]; 
    return total; 
    } 
    public static double averageRain(double[] array, double total) 
    { 
    return total/array.length; 
    } 
    public static int mostRain(double[] array) 
    { 
    double maximum = array[1]; 
    int value = 0; 
    for (int i=0; i < 12; i++) { 
     if (array[i] >= maximum) { 
     maximum = array[i]; 
     value = i; 
     } 
    } 
    return months[index]; 
    } 
    public static int leastRain(double[] array) 
    { 
    double minimum = array[0]; 
    int value = 0; 
    for (int i=0; i < 12; i++) 
    { 
     if (array[i] <= minimum) { 
     minimum = array[i]; 
     value = i; 
     } 
    } 
    return value; 




    } 
} 
+1

它看起來像'mostRain'返回月份名稱。您只需將其返回類型更改爲String。 – Eran 2014-08-29 00:45:19

回答

0

嗯,你已經得到了一個指標,你有幾個月的名稱數組,所以......?

String most = months[mostRain(rain)]; 
+0

謝謝!一個問題是,由於某種原因,現在我在每個月後都有一個1。 – 2014-08-29 00:57:23

0

我想這個代碼是不是連編譯

看到

public static int mostRain(double[] array) 
    { 
    double maximum = array[1]; 
    int value = 0; 
    for (int i=0; i < 12; i++) { 
     if (array[i] >= maximum) { 
     maximum = array[i]; 
     value = i; 
     } 
    } 
    return months[index]; 
    } 

,因爲它是期待返回一個int但你返回String(也索引不存在) ,所以你想要的是

public static int mostRain(double[] array) 
    { 
    double maximum = 0.00; // change this too 
    int value = 0; 
    for (int i=0; i < 12; i++) { 
     if (array[i] >= maximum) { 
     maximum = array[i]; 
     value = i; 
     } 
    } 
    return value; // change this 
    } 

然後你可以本身它像上面這樣

System.out.println("The month with the highest amount of rain is " + months [mostRain (rain)]); 

當然,做出類似的改變也leastRain

+0

謝謝!運行良好!現在我試圖添加一個if語句後,用戶輸入的溫度,如果它的否定。但是,如果它捕捉到負數,它會轉移到下個月,我如何讓用戶重新輸入數據 – 2014-08-29 01:17:18

+0

input = keyboard.nextDouble();如果(輸入<0.00){x--;繼續;} – 2014-08-29 01:23:20