I am writing a java program that receives rainfall for each month. It works fine, but I just need to know how to get the month index - for example, there is an output operator: the month with the lowest rainfall is 1 in 1.6 inches. How to get "1", which is the lowest month index? I can get the lowest rainfall figure, but not an index.
I tried the months [n-1], but I still get the error message "Non-static variable months cannot be specified from a static context."
Any help would be great. Thank you
// EDIT
Here is the code. I tried to play with statics, but it just gave me more errors? So the months [n] at the bottom are where I got stuck.
import java.util.*;
public class Rainfall {
Scanner in=new Scanner(System.in);
int month=12;
double total=0;
double average;
double months[];
public Rainfall()
{
months=new double[12];
}
public void setMonths()
{
for(int n=1; n<=month; n++ )
{
System.out.print("Enter the rainfall (in inches) for month #"+n+": ");
months[n-1] = in.nextDouble();
while (months[n-1] < 0)
{
System.out.print("Rainfall must be at least 0. Please enter a new value.");
months[n-1] = in.nextDouble();
}
}
}
public double getTotalRainFall()
{
total = 0;
for(int i=0; i<12;i++)
{
total=total+months[i];
}
return total;
}
public double getAverageRainFall()
{
average = total/12;
return average;
}
public double getHighestMonth()
{
double highest=0;
for ( int i = 0; i < 12; i++)
{
if ( months[i] > highest)
{
highest = months[i] ;
}
}
return highest;
}
public double getLowestMonth()
{
double lowest = Double.MAX_VALUE;
for ( int n = 0; n < month; n++)
{
if (months[n] < lowest )
{
lowest = months[n];
}
}
return lowest;
}
public static void main(String[]args)
{
Rainfall r =new Rainfall();
r.setMonths();
System.out.println("The total rainfall for this year is " + r.getTotalRainFall());
System.out.println("The average rainfall for this year is " + r.getAverageRainFall());
System.out.println("The month with the highest amount of rain is " + months[n] + "with" + r.getHighestMonth() "inches");
System.out.println("The month with the lowest amount of rain is " + months[n] "with" + r.getLowestMonth() "inches");
}
}
///EDIT # 2 - , . thisYear (.. ). . ?
package Rainfall;
public class Rainfall {
int month = 12;
double total = 0;
double average;
double getRainAt[];
public Rainfall() {
getRainAt = new double[12];
}
double getTotalRain() {
for (int i = 0; i < 12; i++) {
total = total + getRainAt[i];
}
return total;
}
double getAverageRain() {
average = total / 12;
return average;
}
int getHighestMonth() {
int high = 0;
for (int i = 0; i < 12; i++) {
if (getRainAt[i] > getRainAt[high]) {
high = i;
}
}
return high;
}
int getLowestMonth() {
int low = 0;
for (int i = 0; i < 12; i++) {
if (getRainAt[i] < getRainAt[low]) {
low = i;
}
}
return low;
}
public static void main(String[] args) {
double thisYear[] = {1.6, 2.1, 1.7, 3.5, 2.6, 3.7,
3.9, 2.6, 2.9, 4.3, 2.4, 3.7 };
int high;
int low;
Rainfall r = new Rainfall(thisYear);
System.out.println("The total rainfall for this year is " +
r.getTotalRain());
System.out.println("The average rainfall for this year is " +
r.getAverageRain());
high = r.getHighestMonth();
System.out.println("The month with the highest amount of rain " +
"is " + (high+1) + " with " + r.getRainAt(high) +
" inches.");
low = r.getLowestMonth();
System.out.println("The month with the lowest amount of rain " +
"is " + (low+1) + " with " + r.getRainAt(low) +
" inches.");
}
}