(This is Java programming language only) The solution for chapter 5, question #
ID: 3533617 • Letter: #
Question
(This is Java programming language only) The solution for chapter 5, question # 7 PC on this website is an incorrect solution, what I need help for is the correct question #7 which is:
7. Average Rainfall:
Write a program that uses nested loops to collect data and calculate the average rainfall over a period of years. The program should first ask for the number of years. The outer loop will iterate once for each year. The inner loop will iterate twelve 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 chould display the number of months, the total inches of rainfall, and the average rainfall per month for the entire period. (We are using class' with this challenge)
Input validation: Do not acept less than 1 for the number of years. Do not accept negative numbers for the monthly rainfall.
Output:
Enter the number of years: -2
Invalid. Enter 1 or greater: 2
Enter the rainfall, in inches, for each month.
Year 1 month 1: -3
Invalid. Enter 0 or greater: 3
Year 1 month 2: 5
Year 1 month 3: 7
Year 1 month 4: 9
Year 1 month 5: 6
Year1 month 6: 0
Year 1 month 7: 0
Year 1 month 8: 0
Year 1 month 9: 3
Year 1 month 10: 6
Year 1 month 11: 10
Year 1 month 12 8
Year 2 month 1: 7
Year 2 month 2: 6
Year 2 month 3: 10
Year 2 month 4: 8
Year 2 month 5: 4
Year 2 month 6: 0
Year 2 month 7: 0
Year 2 month 8: 0
Year 2 month 9: 4
Year 2 month 10: 15
Year 2 month 11: 12
Year 2 month 12: 5
Number of months: 24
Total rainfall: 128.0 inches
Average monthly rainfall: 5.333333333333333 inches (there are 15 3's)
Explanation / Answer
import java.io.*;
/**
*
* @author Anonymous9292
*/
public class Rainfall {
/**
* @param args the command line arguments
*/
public static void main(String[] args)throws IOException {
// TODO code application logic here
int years;
int month=12;
System.out.println("enter no of years");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
years=Integer.parseInt(br.readLine());
int i=0,j=0;
double count=0;
double sum=0;
while(years<1)
{
System.out.println("enter valid years");
years=Integer.parseInt(br.readLine());
}
int[][] rainfall=new int[years][12];
System.out.println("enter rainfall in inches for each month");
if(years>=1)
{
for(i=0; i<years; i++)
{
for(j=0; j<month; j++)
{
System.out.println("year " + (i+1) + " month" +(j+1) );
rainfall[i][j]=Integer.parseInt(br.readLine());
while(rainfall[i][j]<0)
{
System.out.println("invalid value enter greater than 0");
rainfall[i][j]=Integer.parseInt(br.readLine());
}
count++;
sum+=rainfall[i][j];
}
}
}
double average;
average=sum/count;
System.out.println("no of months "+ count);
System.out.println("totla rainfall "+sum);
System.out.println("average rainfall " + average);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.