Answer to Question #249541 in Java | JSP | JSF for Bonnie

Question #249541

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.


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 (E.g. if col = 0, then print January, col = 1 then print February, etc.) with the corresponding total alongside it.


1
Expert's answer
2021-10-11T05:53:47-0400
import java.util.Scanner;
public class Main
{
	public static void main(String[] args) {
		Scanner in =new Scanner(System.in);
		System.out.println("");
		int [][] arr=new int [12][3];
		int [] townA=new int[12];
		int [] townB=new int[12];
		int [] townC=new int[12];
		for(int i=0;i<12;i++){
		    System.out.println("Month "+(i+1)+": ");
		    for(int j=0;j<3;j++){
		        System.out.println("Town "+(j+1)+": ");
		        arr[i][j]=in.nextInt();
		        if (j==0)
		            townA[i]=arr[i][j];
		        else if (j==1)
		            townB[i]=arr[i][j];
		        else if (j==2)
		            townC[i]=arr[i][j];
		    }
		}
		int totalA=0;
		for(int i=0;i<12;i++){
		    totalA+=townA[i];
		}
		int totalB=0;
		for(int i=0;i<12;i++){
		    totalB+=townB[i];
		}
		int totalC=0;
		for(int i=0;i<12;i++){
		    totalC+=townC[i];
		}
		
		System.out.println("Total rainfall figures for town A: "+totalA);
		System.out.println("Total rainfall figures for town B: "+totalB);
		System.out.println("Total rainfall figures for town C: "+totalC);
		
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS