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.
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);
}
}
Comments
Leave a comment