Answer to Question #249462 in Java | JSP | JSF for Ashwari Pillay

Question #249462

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-11T00:05:55-0400
import java.util.Scanner;
public class Main
{
	public static void main(String[] args) {
	    Scanner input=new Scanner(System.in);
	    int x,number,y;
	    int [] [] rainfall = new int [12] [3];
	    for (int i=0; i<12; i++){
       System.out.println("Enter rainfall of January");
        rainfall[0][0] = input.nextInt();
        rainfall[0][1] = input.nextInt();
        rainfall[0][2] = input.nextInt();
        y=rainfall[0][0]+rainfall[0][1]+rainfall[0][2];
        System.out.print("Average rainfall for the month of January is: "+y);
        System.out.println("\nEnter rainfall of february");
        rainfall[1][0] = input.nextInt();
        rainfall[1][1] = input.nextInt();
        rainfall[1][2] = input.nextInt();
       int f=rainfall[1][0]+rainfall[1][1]+rainfall[1][2];
       System.out.print("Average rainfall for the month of January is: "+f);
		System.out.println("\nEnter rainfall of March");
        rainfall[2][0] = input.nextInt();
        rainfall[2][1] = input.nextInt();
        rainfall[2][2] = input.nextInt();
        int Ma=rainfall[2][0]+rainfall[2][1]+rainfall[2][2];
        System.out.print("Average rainfall for the month of February is: "+Ma);
        System.out.println("\nEnter rainfall of April");
        rainfall[3][0] = input.nextInt();
        rainfall[3][1] = input.nextInt();
        rainfall[3][2] = input.nextInt();
        int A=rainfall[3][0]+rainfall[3][1]+rainfall[3][2];
        System.out.print("Average rainfall for the month of March is: "+A);
        System.out.println("\nEnter rainfall of May");
        rainfall[4][0] = input.nextInt();
        rainfall[4][1] = input.nextInt();
        rainfall[4][2] = input.nextInt();
        int M=rainfall[4][0]+rainfall[4][1]+rainfall[4][2];
        System.out.print("Average rainfall for the month of May is: "+M);
        System.out.println("\nEnter rainfall of June");
        rainfall[5][0] = input.nextInt();
        rainfall[5][1] = input.nextInt();
        rainfall[5][2] = input.nextInt();
        int Ju=rainfall[5][0]+rainfall[5][1]+rainfall[5][2];
        System.out.print("Average rainfall for the month of June is: "+Ju);
        System.out.println("\nEnter rainfall of July");
        rainfall[6][0] = input.nextInt();
        rainfall[6][1] = input.nextInt();
        rainfall[6][2] = input.nextInt();
        int J=rainfall[6][0]+rainfall[6][1]+rainfall[6][2];
        System.out.print("Average rainfall for the month of July is: "+J);
        System.out.println("\nEnter rainfall of August");
        rainfall[7][0] = input.nextInt();
        rainfall[7][1] = input.nextInt();
        rainfall[7][2] = input.nextInt();
        int Aa=rainfall[7][0]+rainfall[7][1]+rainfall[7][2];
        System.out.print("Average rainfall for the month of August is: "+Aa);
        System.out.println("\nEnter rainfall of September");
        rainfall[8][0] = input.nextInt();
        rainfall[8][1] = input.nextInt();
        rainfall[8][2] = input.nextInt();
        int S=rainfall[8][0]+rainfall[8][1]+rainfall[8][2];
        System.out.print("Average rainfall for the month of September is: "+S);
        System.out.println("\nEnter rainfall of October");
        rainfall[9][0] = input.nextInt();
        rainfall[9][1] = input.nextInt();
        rainfall[9][2] = input.nextInt();
        int O=rainfall[9][0]+rainfall[9][1]+rainfall[9][2];
        System.out.print("Average rainfall for the month of October is: "+O);
        System.out.println("\nEnter rainfall of November");
        rainfall[10][0] = input.nextInt();
        rainfall[10][1] = input.nextInt();
        rainfall[10][2] = input.nextInt();
        int N=rainfall[10][0]+rainfall[10][1]+rainfall[10][2];
        System.out.print("Average rainfall for the month of November is: "+N);
        System.out.println("\nEnter rainfall of December");
        rainfall[11][0] = input.nextInt();
        rainfall[11][1] = input.nextInt();
        rainfall[11][2] = input.nextInt();
        int D=rainfall[11][0]+rainfall[11][1]+rainfall[11][2];
        System.out.print("Average rainfall for the month of December is: "+D);
        int E=D+N+y+f+Ma+A+M+Ju+J+Aa+S+O;
        System.out.print("The avarage rainfall for each month is: "+E);
        System.out.println("\nEnter a number");
        number=input.nextInt();
          switch(number) {
            case 0: 
            System.out.println("January");
             break;
            case 1:
            System.out.println("February");
            break;
            case 2: 
            System.out.println("March");
            break;
            case 3:
            System.out.println("April");
            break;
            case 4:
            System.out.println("May");
            break;
            case 5:
            System.out.println("June");
            break;
            case 6:
            System.out.println("July");
            break;
            case 7:
            System.out.println("August");
            break;
            case 8:
            System.out.println("September");
            break;
            case 9:
            System.out.println("October");
            break;
            case 10:
            System.out.println("November");
            break;
            case 11:
            System.out.println("December");
            break;
            default:
            System.out.print("Invalid input");
}
}
}}

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