Here is a sample output:
Enter the Rainfall for month 1.
1
Enter the Rainfall for month 2.
2
Enter the Rainfall for month 3.
3
Enter the Rainfall for month 4.
4
Enter the Rainfall for month 5.
5
Enter the Rainfall for month 6.
6
Enter the Rainfall for month 7.
7
Enter the Rainfall for month 8.
8
Enter the Rainfall for month 9.
9
Enter the Rainfall for month 10.
10
Enter the Rainfall for month 11.
11
Enter the Rainfall for month 12.
12
The total Rainfall for this year is: 78.0
The average for this year is: 6.5
The month with the highest amount of rain is: 0.0
The month with the lowest amount of rain is: 0.0
Here is my current code:
import java.util.Scanner;
public class Rainfall
{
public static void main (String [] args)
{
double[] Rainfall = new double[12]; // The Rainfall data
double most = getHighest(Rainfall);
double least = getLowest(Rainfall);
getValues(Rainfall);
System.out.println("The total Rainfall for this year is: " + getTotal(Rainfall));
System.out.println("The average for this year is: " + getAverage(Rainfall));
System.out.println("The month with the highest amount of rain is: " + most);
System.out.println("The month with the lowest amount of rain is: " + least);
}
/**
getValues method
@return The Rainfall array filled with user input
*/
public static void getValues(double[] array)
{
double input; // To hold user input.
Scanner scan = new Scanner(System.in);
for (int i = 0; i < array.length; i++)
{
System.out.println("Enter the Rainfall for month " + (i + 1) + ".");
array[i] = scan.nextDouble();
}
}
/**
getTotal method
@return The total of the elements in
the Rainfall array.
*/
public static double getTotal(double[] Rainfall)
{
double total = 0.0; // Accumulator
// Accumulate the sum of the elements
// in the Rainfall array.
for (int index = 0; index < Rainfall.length; index++)
//total += Rainfall[index];
total = total + Rainfall[index];
// Return the total.
return total;
}
/**
getAverage method
@return The average of the elements
in the Rainfall array.
*/
public static double getAverage(double[] Rainfall)
{
return getTotal(Rainfall) / Rainfall.length;
}
/**
getHighest method
@return The highest value stored
in the Rainfall array.
*/
public static int getHighest(double[] Rainfall)
{
double highest = Rainfall[0];
int index_of_highest = 0;
for (int index = 1; index < Rainfall.length; index++)
{
if (Rainfall[index] > highest){
highest = Rainfall[index];
index_of_highest = index;
}
}
return index_of_highest;
}
/**
getLowest method
@returns The lowest value stored
in the Rainfall array.
*/
public static int getLowest(double[] Rainfall)
{
double lowest = Rainfall[0];
int index_of_lowest = 0;
for (int index = 1; index < Rainfall.length; index++)
{
if (Rainfall[index] < lowest){
lowest = Rainfall[index];
index_of_lowest = index;
}
}
return index_of_lowest;
}
}

New Topic/Question
Reply


MultiQuote


|