Hey I have a few problems. When I perform calculations on my array it seems sometimes it works like for finding the total or average of inputting all 20's but other times it doesn't. Also I'm trying to set a variable equal to the position of the max and min number in the array. This isn't working I'm not sure if its initializing it or not. Thanks.
import java.util.Scanner; public class RainfallTester { public static void main(String [] args){ mainRain(); } public static void mainRain(){ Scanner scan = new Scanner(System.in); Rainfall temp = new Rainfall(); double rain; for(double j=0; j<temp.rainfall.length;j++ ){ System.out.print("What is the total rainfall for the month:"); rain = scan.nextDouble(); if(rain <= 0){ System.out.println("Error enter a non negative number."); mainRain(); } temp.setRain(rain); } temp.calc(); } }
This is the class with all the proplems
public class Rainfall { int hmonth = 0; int lmonth = 0; double rainfall[] = new double[12]; public void setRain(double rain){ for(int i =0;i<rainfall.length;i++){ rainfall[i] = rain; } } public void calc(){ double sum =0; double avg = 0; double max = rainfall[0]; double min = rainfall[0]; //find total for(int i =0; i<rainfall.length;i++){ sum += rainfall[i]; } System.out.println("The total rainfall is "+sum); //find average avg = sum/rainfall.length; System.out.println("The average rain fall is "+avg); //find max for (int i=0; i<rainfall.length;i++){ if (max < rainfall[i]){ max = rainfall[i]; hmonth = i; } } System.out.print("The month with the most rain is "); mostMonth(); System.out.println("with "+max+" rain"); //find min for (int i=0; i<rainfall.length;i++){ if (min > rainfall[i]){ min = rainfall[i]; lmonth = i; } } System.out.print("The month with the least rain is "); leastMonth(); System.out.println("with "+min+" rain"); } //find most month public void mostMonth(){ if(hmonth == 0){ System.out.print("January "); }else if(hmonth == 1){ System.out.print("Feburary "); }else if(hmonth == 2){ System.out.print("March "); }else if(hmonth == 3){ System.out.print("April "); }else if(hmonth == 4){ System.out.print("May "); }else if(hmonth == 5){ System.out.print("June "); }else if(hmonth == 6){ System.out.print("July "); }else if(hmonth == 7){ System.out.print("August "); }else if(hmonth == 8){ System.out.print("September "); }else if(hmonth == 9){ System.out.print("October "); }else if(hmonth == 10){ System.out.print("November "); }else{ System.out.print("December "); } } //find least month public void leastMonth(){ if(hmonth == 1){ System.out.print("January "); }else if(hmonth == 2){ System.out.print("Feburary "); }else if(hmonth == 3){ System.out.print("March "); }else if(hmonth == 4){ System.out.print("April "); }else if(hmonth == 5){ System.out.print("May "); }else if(hmonth == 6){ System.out.print("June "); }else if(hmonth == 7){ System.out.print("July "); }else if(hmonth == 8){ System.out.print("August "); }else if(hmonth == 9){ System.out.print("September "); }else if(hmonth == 10){ System.out.print("October "); }else if(hmonth == 11){ System.out.print("Novenmber "); }else{ System.out.print("December "); } } }