0

I am working on an assignment for school to compute the average test score from given inputs. It does not exit the do/while loop when I type in "-1". The count does not increment when I plug in a value and as such the average does not calculate correctly.

  1. Create a class called Average.
  2. Try to use separate sections in the main method to declare all variables, get input, do processing, and perform output. Create a comment for each section.
  3. In the declarations section of main, declare an double variable called average.
  4. In the input section of main, display an opening message using JOptionPane that explains the purpose of the program.
  5. In the processing section of main, assign the value of average by calling a method named calcAverage().
  6. Within the calcAverage method, declare int variables count, score, and total, and a double variable called average.
  7. Also in the calcAverage method, use a do-while loop to get scores from the user until the user enters -1 to quit. (Your message to the user should be, "Enter a score or -1 to quit".)
  8. In the calcAverage method, calculate the average score and return that value to the main method.
  9. In the output section of main, display a JOptionPane window that states (for example), "The average of the 5 scores is 75.8."
import javax.swing.JOptionPane;

public class Average {
    static int count = 0;

    public static void main(String[] args) {
        //Declaration section
        double average;
        //Input Section
        calcAverage();
        //Processing Section
        average = calcAverage();
        //Output Section
        JOptionPane.showMessageDialog(null, "The average of the " + count + "scores is" + average);
    } // end main

    public static double calcAverage() {
        int count = 0, score = 0, total = 0;
        double average;
        String input;
        do {
            input = JOptionPane.showInputDialog("Enter a score or -1 to quit");
            score = Integer.parseInt(input);
            total = total + score;
            count++;
        } while (score != -1);
        average = (total / count);
        return average;
    } // end calcAverage
} // end class
Tom
  • 16,842
  • 17
  • 45
  • 54
Michael Meis
  • 27
  • 1
  • 6

1 Answers1

3

Your count local variable in calcAverage is shadowing the count variable you declared static in the Average class, so the static variable is never updated; it remains 0. Don't re-declare this variable in calcAverage.

You are calling calcAverage twice, and ignoring what is is returning the first time. Because of this, you have to enter -1 twice to finish the program. Remove the first calcAverage call.

You always count the number, even if -1 is entered, because the condition isn't checked until the end of the loop. Add an if condition to update total and count only if score isn't -1.

You have integer division on this line:

average = (total / count);

In Java, division of integers yields an integer, so the average of 2 numbers 5 and 6 would be 5, not 5.5. Cast one of them to double.

average = ((double) total / count);
rgettman
  • 176,041
  • 30
  • 275
  • 357
  • As the asker of this question, you may, at your discretion, [accept exactly one answer](http://stackoverflow.com/help/someone-answers) that you think helps you the best. – rgettman Oct 27 '15 at 21:45