2016-12-05 128 views
-5

我需要編寫一個程序,用於存儲12個月的總降雨量,然後顯示總降水量,平均降水量,最高降雨量和最低降雨量的月份輸出。我的程序運行,但我不知道如何使輸出的月份數量而不是當月的降雨量。 例如,當我有時,輸入6個月下雨:15.6我的輸出應該是6而不是15.6。謝謝你的幫助。如何打印數組的最大值和最小值?

import java.util.Scanner; 

public class Rainfall 
{ 
    public static void main (String [] args) 
    { 
     double[] rain = new double[12]; // The rainfall data 

     getValues(rain); 
     showValues(rain); 
     System.out.println("The total rainfall for this year is: " + totalRain(rain)); 
     System.out.println("The average rainfall for this year is: " + averageRain(rain)); 
     System.out.println("The month with the highest amount of rain is: " + mostRain(rain)); 
     System.out.println("The month with the lowest amount of rain is: " + leastRain(rain)); 
    } 

    /** 
     showValues method 
     @param rain Display the rainfall array 
    */ 
    public static void showValues(double[] rain) 
    { 

    } 

    /** 
     getValues method 
     @return The rain array filled with user input 
    */ 
    public static void getValues(double[] array) 
    { 
      Scanner scan = new Scanner(System.in); 

      for (int i = 0; i < array.length; i++) 
      { 
       System.out.println("Enter rain fall for month " + (i + 1) + "."); 
       array[i] = scan.nextDouble(); 
      } 
    } 

    /** 
     getTotal method 
     @return The total of the elements in 
     the rainfall array. 
    */ 

    public static double totalRain(double[] rain) 
    { 
     double total = 0.0;  // Accumulator 

     // Accumulate the sum of the elements 
     // in the rainfall array. 
     for (int index = 0; index < rain.length; index++) 
     //total += sales[index]; 
     total = total + rain[index]; 

     // Return the total. 
     return total; 
    } 

    /** 
     getAverage method 
     @return The average of the elements 
     in the rainfall array. 
    */ 

    public static double averageRain(double[] rain) 
    { 
     return totalRain(rain)/rain.length; 
    } 

    /** 
     getHighest method 
     @return The highest value stored 
     in the rainfall array. 
    */ 

    public static double mostRain(double[] rain) 
    { 
     double highest = rain[0]; 

     for (int index = 1; index < rain.length; index++) 
     { 
     if (rain[index] > highest) 
      highest = rain[index]; 
     } 

     return highest; 
    } 

    /** 
     getLowest method 
     @returns The lowest value stored 
     in the rainfall array. 
    */ 

    public static double leastRain(double[] rain) 
    { 
     double lowest = rain[0]; 

     for (int index = 1; index < rain.length; index++) 
     { 
     if (rain[index] < lowest) 
      lowest = rain[index]; 
     } 

     return lowest; 
    } 
} 

回答

0

下面是一個使用JAVA 8流一個非常簡單的方法:

Double[] doubles = new Double[12]; 


     List<Double> doubleList = Arrays.asList(doubles); 


     Double max = doubleList.stream().mapToDouble(dub -> dub).max().getAsDouble(); 

     Double min = doubleList.stream().mapToDouble(dub -> dub).min().getAsDouble(); 
0

試試這個:取而代之的是給定索引處的雨水量,你想要索引istelf。

public static double mostRain(double[] rain) 
    { 
     double highest = rain[0]; 
     int highestMonth = 0; 

     for (int index = 1; index < rain.length; index++) 
     { 
     if (rain[index] > highest) 
      highest = rain[index]; 
      highestMonth = index; 
     } 

     return highestMonth; 
    } 
相關問題