Write a java program where a user enters and stores the average monthly rainfall
ID: 3742435 • Letter: W
Question
Write a java program where a user enters and stores the average monthly rainfall figures (int) for three (3) towns over a year. The rows represent the months, and the columns represent the towns TOWN 1 TOWN 2 TOWN Month1 Month2 Month3 Month4 Month5 Month6 Month7 Month8 Month9 Month10 Month11 Month12 Calculate and print the total rainfall figures for each town, and the average rainfall for each month, Use a switch statement to print out the month name according to the column subscript value (Eg. if col 0, then print January, col 1 then print February, etc.) with the corresponding total alongside it. Example of output: SUMMARY OF RAINFALL FOR 2016 Town 1 total rainfall Town 2 total rainfall Town 3 total rainfall 175 mm 2345 mm 689 mm January February March etc 409 mm 560 mm 157 mmExplanation / Answer
If you have any doubts, please give me comment...
import java.util.Scanner;
public class RainFall{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int rainfall[][] = new int[3][12];
int towns[] = new int[3];
for(int i=0; i<3; i++){
System.out.println(" Enter Town "+(i+1)+" rainfalls: ");
for(int j=0; j<12; j++){
System.out.print(getMonth(j)+": ");
rainfall[i][j] = in.nextInt();
}
}
System.out.println("SUMMARY OF RAINFALL FOR 2016: ");
System.out.println(" TOWN1 TOWN2 TOWN 3 Total");
for(int i=0; i<12; i++){
System.out.printf("%15s", getMonth(i));
int sum = 0;
for(int j=0; j<3; j++){
System.out.print(" "+rainfall[j][i]);
sum += rainfall[j][i];
towns[j] += rainfall[j][i];
}
System.out.println(" "+sum);
}
System.out.printf("%15s","Total");
for(int i=0; i<3; i++)
System.out.print(" "+towns[i]);
System.out.println();
}
1
public static String getMonth(int monthNo){
switch(monthNo){
case 0:
return "January";
case 1:
return "February";
case 2:
return "March";
case 3:
return "April";
case 4:
return "May";
case 5:
return "June";
case 6:
return "July";
case 7:
return "August";
case 8:
return "September";
case 9:
return "October";
case 10:
return "November";
case 11:
return "December";
}
return "";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.