I'm coding a test averager in Java. I have to use do while, while, if loops.
I have the user inputting test scores, asking for a negative number to leave the while loop.
I have to display the number of scores entered, the highest, the lowest, and the average.
However, I am having problems displaying the lowest number as it as popping up as 0.
For input of 95, 93, 92, 91, and -1:
The number of scores entered: 4 The Highest: 95 The Lowest: 0 The average is: 92
My code:
import java.util.*;
public class Lab7
{
public static void main(String[] args)
{
System.out.println("This program computes the average of");
System.out.println("a list of (nonnegative) exam scores.");
int sum;
int numberOfTests;
int testInput;
String answer;
int minimum = 0;
int maximum = 0;
Scanner keyboard = new Scanner (System.in);
do
{
System.out.println();
System.out.println("Enter all the scores to be averaged.");
System.out.println("Enter a negative number after");
System.out.println("you have entered all the scores.");
sum = 0;
numberOfTests = 0;
testInput = keyboard.nextInt();
while (testInput >= 0)
{
sum = sum + testInput;
numberOfTests++;
if (testInput < minimum)
{
minimum = testInput;
}
if (testInput > maximum)
{
maximum = testInput;
}
testInput = keyboard.nextInt();
}
if (numberOfTests > 0)
{
System.out.println("The number of scores entered: " + numberOfTests);
System.out.println("The Highest: " + maximum);
System.out.println("The Lowest: " + minimum);
System.out.println("The average is: " +
(sum / numberOfTests));
}
else
{
System.out.println("No scores to average.");
}
System.out.println("Want to average another exam?");
System.out.println("Enter yes or no.");
answer = keyboard.next();
}
while (answer.equalsIgnoreCase("yes"));
}
}