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, and the months with the highest and lowest amounts.

Respuesta :

ijeggs

Answer:

import java.util.Arrays;

import java.util.Scanner;

public class num12 {

   public static void main(String[] args) {

       double [] rainfall = new double[12];

       Scanner in = new Scanner(System.in);

       System.out.println("Enter the rainfall for the first month");

       rainfall[0]= in.nextDouble();

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

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

           rainfall[i]= in.nextDouble();

       }

       System.out.println("Enter the rainfall for the last month");

       rainfall[11]=in.nextDouble();

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

       int totalRainfall = 0;

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

            totalRainfall += rainfall[i];

        }

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

       System.out.println("The Average Monthly Rainfall is "+(totalRainfall/12));

       double minRain = rainfall[1];

       int monthLowest =0;

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

           if(minRain>rainfall[i]){

               minRain = rainfall[i];

               monthLowest =i;

           }

       }

       System.out.println("The Minimum Rainfall is: "+minRain+ " In the "+(monthLowest+1) +" Month");

       double maxRain = rainfall[1];

       int monthHighest =0;

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

           if(maxRain<rainfall[i]){

               maxRain = rainfall[i];

               monthHighest =i;

           }

       }

       System.out.println("The Maximum Rainfall is: "+maxRain+" In the "+(monthHighest+1)+" Month");

   }

}

Explanation:

Using Java programming Language

  • An Array is created to hold the list of total monthly rainfall.
  • Scanner is used to prompt user to enter the monthly rainfall
  • Using a for loop the user is prompted to repetitively enter rainfall for each month
  • The list is displayed using Java's Arrays.toString Method
  • The total rainfall is created by adding elements at index 0-11 using a for loop
  • The montly average is calculated by dividing total rainfall by 12 totalRainfall/12
  • Using a combination of  for loops and if statements and creating variables for Min and Max rainfall we determine the maximum and minimum months of rainfall and output them

ACCESS MORE