Students: This content is controlled by your instructor, and is not zyBooks cont
ID: 3811455 • Letter: S
Question
Students: This content is controlled by your instructor, and is not zyBooks content. Direct questions or concerns about this content to your instructor. If you have any technical issues with the zyLab submission system, use the "Trouble with lab?" button at the bottom of the lab. Objectives To practice nested loops To earn extra credit Assignment Extra Credit B Write a Java program that uses nested loops to collect data and calculate the average rainfall over a period 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 the 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. Requirements: Do not accept a number less than 1 for the number of years. Print "Must enter a number 1 or greater" and stop the program. Do not accept negative numbers for the monthly rainfall. Print "Rainfall must be 0 or greater" and stop program. The prompt for years should read: "Enter the number of years:" The prompt for months should read: "Enter the inches of rainfall for month n:" Samples of program execution (as if run on UNIX): > java ExtraCreditB Enter the number of years: 2 Enter the inches of rainfall for month 1: 2.4 Enter the inches of rainfall for month 2: 3.1 Enter the inches of rainfall for month 3: 4.7 Enter the inches of rainfall for month 4: 1.3 ... Total number of months: 24 Total inches of rainfall: Average rainfall per month:Explanation / Answer
Program:-
import java.util.*;
public class HelloWorld{
public static void main(String []args){
System.out.println("Enter number of years: ");
Scanner sc = new Scanner(System.in);
int year = sc.nextInt(); // input from user for number of year
if(year<1){
System.out.println("Must enter a number 1 or greater");
System.exit(0);
}
double month;
double sum=0;
int num=0;
for(int i=1;i<=year;i++){ // outer loop
for(int j=1;j<=12;j++){ // inner loop
System.out.println("Enter the inches of rainfall for month "+j+": ");
month = sc.nextDouble();
if(month<0){
System.out.println("Rainfall must be 0 or greater");
System.exit(0);
}
sum+=month;
num=num+1;
}
}
System.out.println("Total number of months: "+num);
System.out.println("Total inches of rainfall: "+sum);
System.out.println("Average inches per month: "+sum/num);
}
}
Output:-
Enter number of years:
1
Enter the inches of rainfall for month 1:
10
Enter the inches of rainfall for month 2:
10
Enter the inches of rainfall for month 3:
2
Enter the inches of rainfall for month 4:
2.5
Enter the inches of rainfall for month 5:
6.7
Enter the inches of rainfall for month 6:
3.9
Enter the inches of rainfall for month 7:
1.8
Enter the inches of rainfall for month 8:
4
Enter the inches of rainfall for month 9:
3
Enter the inches of rainfall for month 10:
2
Enter the inches of rainfall for month 11:
1
Enter the inches of rainfall for month 12:
2
Total number of months: 12
Total inches of rainfall: 48.9
Average inches per month: 4.075