Design a program that lets the user enter the total rainfall for each of 12 months into a list. The program should calculate and display the total rainfall for the year, the average monthly rainfall, the months with the highest and lowest amounts.

Respuesta :

ijeggs

Answer:

import java.util.Arrays;

import java.util.Scanner;

public class num1 {

   public static void main(String[] args) {

       Scanner in = new Scanner (System.in);

      int []monthlyRainfall = new int [12];

       System.out.println("Enter rainfall for January");

       monthlyRainfall[0]= in.nextInt();

      for(int i = 1; i<monthlyRainfall.length; i++){

          System.out.println("Enter rainfall for the next Month");

          monthlyRainfall[i]= in.nextInt();

      }

       System.out.println("The complete list is:");

       System.out.println(Arrays.toString(monthlyRainfall));

       //Calculating the total and average rainfall

       int totalRainfall =0;

       for(int i=0; i<monthlyRainfall.length; i++){

           totalRainfall+=monthlyRainfall[i];

       }

       System.out.println("Total rainfall is "+ totalRainfall);

       System.out.println("Average rainfall is "+(totalRainfall/12));

       // finding the lowest rainfall

       int min = monthlyRainfall[0];

       int minMnth =0;

       for(int i=0; i<monthlyRainfall.length; i++){

           if(min>monthlyRainfall[i]){

              min = monthlyRainfall[i];

               minMnth =i+1;

           }

       }

       System.out.println("The minimim rainfall is "+min);

       System.out.println("The month with the minimum rainfall is "+minMnth);

       // finding the highest rainfall

       int max = monthlyRainfall[0];

       int maxMnth =0;

       for(int i=0; i<monthlyRainfall.length; i++){

           if(max<monthlyRainfall[i]){

               max = monthlyRainfall[i];

               maxMnth = i+1;

           }

       }

       System.out.println("The maximum rainfall is "+max);

       System.out.println("The month with the maximum rainfall is "+maxMnth);

   }

}

Explanation:

  • Create an array to hold the list of monthly rainfall from january to December int []monthlyRainfall = new int [12];
  • Prompt user to enter the values for the monthly rainfall and fill up the array with the values using a for loop
  • Print the populated List
  • Use a for loop to calculate the Total rainfall by adding elements from index 0-11
  • Calculate the average rainfall totalrainfal/12
  • Using a for loop, find the maximum and minimum values in the array
  • The index of the minimum value plus 1 Gives the month with the lowest rainfall
  • The index of the maximum value +1 gives the month with the highest value
ACCESS MORE