I have this program where the user inputs x amount of grades, then the program divides the sum of the grades by the number of inputs taken. It should also find the highest and lowest grades entered. When the user is finished, he/she types in -1 to end the program. However, I am having some issues with it.
- The program adds -1 to the average which skews all my results.
- I can't figure out how to calculate the minimum value in the list. It always counts -1 as the minimum value.
Here is the code below:
import java.util.Scanner;
public class Lab4_EnteringGrades
{
public static void main (String a[])
{
Scanner kb = new Scanner(System.in);
double average = 0;
double gradesSum = 0;
int grade = 0;
int numTests = 0;
int maxValue = 0;
int minValue = 0;
while (grade != -1)
{
gradesSum += grade;
numTests += 1;
average = gradesSum / numTests;
System.out.print("Enter a grade: ");
grade = kb.nextInt();
while ((grade < -1) || (grade > 100))
{
System.out.println("Invalid grade.");
System.out.print("Enter a grade: ");
grade = kb.nextInt();
}
if (grade > maxValue)
maxValue = grade;
if (grade < maxValue)
minValue = grade;
}
System.out.printf("\nTotal number of tests: %d", numTests);
System.out.printf("\nThe average is %.1f", average);
System.out.printf("\nThe max value is %d", maxValue);
System.out.printf("\nThe min value is %d", minValue);
}
}