Write a program that uses nested loops to collect data and calculate .docx
1. Write a program that uses nested loops to collect data and
calculate the average rainfall over a period of years. First the
program should ask for the number of years. The outer loop will
iterate once for each year. The inner loop will iterate 12 times,
once for each month. Each iteration of the inner loop will ask
the user for inches of rainfall for that month. After all
iterations, the program should display the number of months,
the total inches of rainfall, and the average rainfall per month
for the entire period. Input validation: Do not accept a number
less than 1 for the number of years. Do not accept negative
numbers for the monthly rainfall.
Solution
import java.util.Scanner;
import java.text.*;
public class AverageRainfall {
public static void main(String[] args) {
int years; // Variable to hold the amount of
years
final int MONTHS = 12; // Constant to hold the
amount of months in a year
2. int totalMonths = 0; // Months accumulator
variable
double monthlyRain; // Holds the amount of
rain in inches for each month
double totalRainfall = 0; // Holds the
total rainfall
NumberFormat df =
DecimalFormat.getInstance(); // Decimal
formating
df.setMaximumFractionDigits(2);
// Create Scanner object
Scanner input = new Scanner(System.in);
// Prompt user for the number of years
System.out.println("Enter the number of
years: ");
years = input.nextInt();
System.out.println("Enter the rainfall amount
for each month ");
3. // Construct for-loop for each year
for(int i=0; i < years; i++){
for(int j=1; j<MONTHS+1; j++){
switch(j){
case 1:
System.out.print("January: ");
break;
case 2:
System.out.print("February: ");
break;
case 3:
System.out.print("March: ");
break;
case 4:
System.out.print("April: ");
break;
case 5:
System.out.print("May: ");
break;
case 6:
4. System.out.print("June: ");
break;
case 7:
System.out.print("July: ");
break;
case 8:
System.out.print("August: ");
break;
case 9:
System.out.print("September: ");
break;
case 10:
System.out.print("October: ");
break;
case 11:
System.out.print("November: ");
break;
case 12: