Dayaan M. answered 2d
Computer Science Graduate with Computer Engineering Coursework
The no arrays rule is the whole point of this exercise. You never need to store the scores, you just need to keep a few running totals and update them as each score comes in. Here is the full program.
import java.util.Scanner;
public class QuizScores {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int count = 0;
int sum = 0;
int highest = 0;
int lowest = 0;
System.out.print("Enter a quiz score (99 to quit): ");
int score = input.nextInt();
while (score != 99) {
if (score < 0 || score > 10) {
System.out.println("Score must be between 10 and 0");
} else {
if (count == 0) {
highest = score;
lowest = score;
} else {
if (score > highest) { highest = score; }
if (score < lowest) { lowest = score; }
}
count++;
sum += score;
}
System.out.print("Enter a quiz score (99 to quit): ");
score = input.nextInt();
}
if (count == 0) {
System.out.println("No valid scores were entered.");
} else {
double average = (double) sum / count;
System.out.println("Valid scores entered: " + count);
System.out.println("Highest score: " + highest);
System.out.println("Lowest score: " + lowest);
System.out.println("Average score: " + average);
}
}
}
The four things that usually break this one.
1. Starting highest and lowest at 0. This is the big one. If you write int lowest = 0 and then compare, nothing will ever be smaller than 0, so the lowest always stays 0 even when the real minimum is 7. Instead I take the first valid score and use it as both the highest and the lowest, then compare from there. That is what the count == 0 check is doing.
2. Integer division on the average. If sum and count are both int, then sum / count throws away the decimal. With a sum of 23 over 3 scores Java gives you 7, not 7.67. Casting one side with (double) fixes it, and the cast has to be there before the division happens, not after.
3. Validating the sentinel. Read the score, then check it against 99 before the range check. Otherwise 99 gets caught by the "greater than 10" test, prints the error message, and your quit value looks like a bad score.
4. Dividing by zero. If the very first thing typed is 99, count stays 0 and sum / count crashes with an ArithmeticException. The if (count == 0) guard at the end handles it.
I compiled and ran this to check all of it. Entering 5, 8, 10, 99 gives 3 scores with a high of 10, a low of 5 and an average of 7.666666666666667. Entering 5, -3, 12, 8, 99 prints the error twice and reports 2 valid scores. Typing 99 straight away prints the no valid scores message instead of crashing. And a set including a 0 correctly reports 0 as the lowest, which is the case that catches the bug in point 1.