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.
- Create a class called Average.
- 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.
- In the declarations section of main, declare an double variable called average.
- In the input section of main, display an opening message using JOptionPane that explains the purpose of the program.
- In the processing section of main, assign the value of average by calling a method named calcAverage().
- Within the calcAverage method, declare int variables count, score, and total, and a double variable called average.
- 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".)
- In the calcAverage method, calculate the average score and return that value to the main method.
- 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