eclipse 尝试更正“getAverageRainfall”以显示使用“getTotalRainfall”除以“MONTHS”的输入平均值

lyr7nygr  于 2022-12-12  发布在  Eclipse
关注(0)|答案(1)|浏览(98)

规格型号:编写一个程序,将12个月中每个月的总降雨量存储在一个数组中。该程序应具有以下单独的方法:
getRainfall -以数组作为参数,从用户处读取12个月中每个月的降雨量(以英寸为单位),并将值存储在数组中。该方法不接受用户提供的负数。
displayRainfall -将包含每月降雨量(英寸)的数组作为参数,并显示每个月的降雨量。
getTotalRainfall -将包含每月降雨量(英寸)的数组作为参数,并返回一年的总降雨量(英寸)。
getAverageRainfall -将包含每月降雨量(英寸)的数组作为参数,并返回每月的平均英寸数。
getRainfallAbove -将包含每月降雨量(英寸)的数组和一个数字作为参数,并返回降雨量超过数字参数的月份数。
在main方法中声明数组,调用每个单独的方法,并在main中输出结果,以演示整个程序的工作状态。
我的代码:

public static final int MONTHS = 12;
    static double[] rain = new double[MONTHS]; //Crate an array to hold the rain values
    
    public static void main(String[] args) throws IOException {
        
        
        
        rain = getRainfall(args); 
        displayRainfall(rain);
        getTotalRainfall(rain); 
        getAverageRainfall(rain);

   }
   
       public static double[] getRainfall(String[] args) throws IOException {
            String[] months = { "January", "February", "March", "April", "May", "June", "July",
                                "August", "September", "October", "November", "December" }; //Each month in a year

               //Crate an array to hold the rain values
               double[] rain = new double[12];

               //Create a Scanner object for keyboard input.
               Scanner keyboard = new Scanner(System.in);
               
               //Get rain values and store them in the rain array
               for (int i = 0; i < months.length; i++)
                   {          
                   System.out.print("Enter rainfall for " + months[(i)] + ": ");
                   rain[i] = keyboard.nextDouble();
                   if (rain[i] <0)
                   {
                       System.out.println("You can not use a negative number.\n");
                       i--;
                   }
               }
         return rain;
        }
       
        private static void displayRainfall(double[] rain) {
            String[] months = { "January", "February", "March", "April", "May", "June", "July",
                    "August", "September", "October", "November", "December" }; //Each month in a year
            
            System.out.println("\nDisplaying rainfall for each month\n");
            for(int i = 0; i < months.length; i++) {
                   System.out.print("Rainfall for " + months[i] + ": " + rain[i] + "\n");
            } 
        }
            
                private static double getTotalRainfall(double[] rain) {
                    {
                        double total = 0.0; //Accumulator

                        //Get sum of all values in the rain array.
                        for (double value: rain)
                            total += value;
                        System.out.printf("\nThe total rainfall for the year is: " + total);

                        return total;
                    }
                }
                    private static double getAverageRainfall(double[] rain) {
                        {
                            double average = 0.0;
                            average += getTotalRainfall(rain)/ MONTHS;
                            return average;
                        }

输出量:

Rainfall for April: 1.0
Rainfall for May: 1.0
Rainfall for June: 1.0
Rainfall for July: 1.0
Rainfall for August: 1.0
Rainfall for September: 1.0
Rainfall for October: 1.0
Rainfall for November: 1.0
Rainfall for December: 1.0

The total rainfall for the year is: 12.0
The total rainfall for the year is: 12.0
vuktfyat

vuktfyat1#

您必须返回rain并在display方法中使用它

public static void main(String[] args) throws IOException {     
         double[] rain = getRainfall(args); 
         displayRainfall(rain);     
   }
    
        public static void getRainfall(String[] args) throws IOException {
             String[] months = { "January", "February", "March", "April", "May", "June", "July",
                                 "August", "September", "October", "November", "December" }; //Each month in a year

            //Crate an array to hold the rain values
            double[] rain = new double[12];

                //Create a Scanner object for keyboard input.
                Scanner keyboard = new Scanner(System.in);
                
                //Get rain values and store them in the rain array
                for (int i = 0; i < months.length; i++)
                    {          
                    System.out.print("Enter rainfall for " + months[(i)] + ": ");
                    rain[i] = keyboard.nextDouble();
                    if (rain[i] <0)
                    {
                        System.out.println("You can not use a negative number.\n");
                        i--;
                    }
                }
          return rain;
        }
    private static void displayRainfall(double[] rain) {
       String[] months = { "January", "February", "March", "April", "May", "June", "July",
                         "August", "September", "October", "November", "December" }; //Each month in a year
                 
       System.out.println("\nDisplaying rainfall for each month\n");
       for(int i = 0; i < months.length; i++) {
            System.out.print("Rainfall for " + months[i] + ": " + rain[i] + "\n,  ");
       }
  }

相关问题