Blake Brennan Blake Brennan - 6 months ago 7
Java Question

Trouble with Choice Menu and user generated input.

I need to make a program that takes user generated monthly rainfall numbers and and averages them, finds the max and min, finds standard deviation etc. I seem to have most everything down but my "find numbers above and below the average" doesn't seem to be working. Also i cant figure out how to let the user make another choice once they have selected and then been given the desired info. Any help would be greatly appreciated.
example output

import java.util.Arrays;
import java.util.Scanner;

public class RainfallAnalyzer {

public static void main(String[] args) {
double theRainfall = 0;
double total = 0;

Scanner scan = new Scanner(System.in);

double[] rainfall = new double[12];

System.out.println("Enter the rainfall measurements for this year: ");

for (int i = 0; i < rainfall.length; i++) {
theRainfall = scan.nextDouble();

rainfall[i] = theRainfall;
total += rainfall[i];

}

int choiceentry = -1;

loop:
while (choiceentry <= 1 || choiceentry > 8) {

System.out.println("1. print all rainfall measurements");
System.out.println("2. print the avererage rainfall");
System.out.println("3. print the maximum and minimum rainfall measurements");
System.out.println("4. print the number of months above average rainfall");
System.out.println("5. print the number of months below average rainfall");
System.out.println("6. print the median rainfall measurement");
System.out.println("7. print the standard deviation");
System.out.println("8. exit");

if (scan.hasNextInt()) {
choiceentry = scan.nextInt();
}

switch (choiceentry) {
case 1:
for (int i = 0; i < rainfall.length; i++) {
System.out.println("Month # " + (i + 1) + ": " + rainfall[i]);
}
break loop;
case 2:
total = total / 12;
System.out.println("This year's average rainfall: " + total);
break loop;

case 3:
Arrays.sort(rainfall);
System.out.println("Min value " + rainfall[0]);
System.out.println("Max value " + rainfall[rainfall.length - 1]);
break loop;

case 4:
int numRainfallAbove = 0;
for (int i = 0; i < rainfall.length; i++) {
if (rainfall[i] >= avg) {
numRainfallAbove++;
} else {
}
}
System.out.println("Number of months above the average rainfall: " + numRainfallAbove);
break loop;

case 5:
int numRainfallBelow = 0;
for (int i = 0; i < rainfall.length; i++) {
if (rainfall[i] <= total) {
numRainfallBelow++;
} else {
}
}
System.out.println("Number of months below the average rainfall: " + numRainfallBelow);
break loop;

case 6:
double median;
if (rainfall.length % 2 == 0) {
median = ((double) rainfall[rainfall.length / 2] + (double) rainfall[rainfall.length / 2 - 1]) / 2;
} else {
median = (double) rainfall[rainfall.length / 2];
}

System.out.println("This year's median rainfall: " + median);

break loop;

case 7:
double theDeviation = deviation(rainfall, total);
System.out.println("The standard deviation is " + theDeviation);
break loop;

}

}

}

public static double deviation(double[] rainfall, double total) {
double endResult = 0;
double temporary = 0;
double arraySum = 0;
double average = total;
double sum = 0;

for (int i = 0; i < rainfall.length; i++) {
sum += Math.pow((rainfall[i] - average), 2);
}

temporary = sum / (rainfall.length - 1);
endResult = Math.sqrt(temporary);
return endResult;
}
}

Answer

Change the code within your case 4:

int numRainfallAbove = 0;
for(int i = 0; i < rainfall.length; i++){
if(rainfall[i] >= total){
numRainfallAbove++;
} else{}
}
System.out.println("Number of months above the average rainfall:   "+numRainfallAbove);
break loop;

To this:

int numRainfallAbove = 0;
double avg = total / 12;
for (int i = 0; i < rainfall.length; i++) {
  if (rainfall[i] >= avg) {
    numRainfallAbove++;
  }
}
System.out.println("Number of months above the average rainfall:   " + numRainfallAbove);
break loop;

The issue is that you compare the monthly rainfall to the total instead of the average. You will need to do something similar for your "number of months with below average rainfall".

To make it keep asking for more choices as long as the user doesn't exit, change the while loop to:

while (choiceentry != 8) {

As an aside that does not directly relate to your question, try to indent your code properly when you post, and DO NOT use goto statements or breaks which direct to labels.

Comments