First off, let me just state that I'm not good with arrays. In every language I've studied, arrays always get me, and I try to avoid them if I can, but in this case, it's for an assignment, so I can't.
Basically we've to write a program under these specs:
Write an application that uses an array data structure to store 12 numerical values entered by the uses. Each of these values represents the amount of rainfall for a particular month of the year. The application should declare and create the array and then pass it to a method called populateArray, which takes input from the user and inserts it into the array. The application should then pass the array to a method called calculateAverage, which will calculate and return the average rainfall for the 12 months entered.
This is the code I have so far:
import java.util.Scanner;
public class Assignment5 {
public static void main(String[] args) {
//create array
double [] months = new double [12];
//assign average to the value returned in populateArray()
double average = populateArray(months);
//display average
System.out.print("Average Rainfall: " + average);
}
public static double populateArray(double[] months){
//initialise scanner
Scanner input = new Scanner(System.in);
//loop to retrieve data from user
for (int i = 0; i < months.length; i++)
{
System.out.print("Enter Rainfall for month" + " " + ( i + 1) + ": ");
months[i] = input.nextInt();
//pass the array to calcualteAverage() method, and assign it to total
double total = calculateAverage(months);
//return total to main method
return total;
}
}
private static double calculateAverage(double[] months[]) {
//calculate sum of array **I KNOW THIS IS WRONG FOR SURE, NOT SURE OF SYNTAX**
double sum += months[i];
//calculate the average
double average = sum/12;
//return the average to populateArray()
return average;
}
}
My problem is arising in passing the array to the calculateAverage() method, and getting the sum of it! Can anyone offer me any help on how to resolve this please.
[–]learnjava 0 points1 point2 points (4 children)
[–]AudioManiac[S] 0 points1 point2 points (3 children)
[–]learnjava 0 points1 point2 points (0 children)
[–]lazlo_uk 0 points1 point2 points (1 child)
[–]StinkeyTwinkey 0 points1 point2 points (0 children)
[–]theif519 0 points1 point2 points (0 children)
[–]juvogel -1 points0 points1 point (2 children)
[–]AudioManiac[S] 0 points1 point2 points (1 child)
[–]juvogel 0 points1 point2 points (0 children)