import java.util.Scanner;
import java.util.ArrayList;
public class Test2
{
public static void main(String[]args)
{
final int SENTINEL = 99;
double newValue;
int counter = -1;
double sumOfValues = -99;
double avg;
ArrayList scoresList = new ArrayList();
// the program was counting the SENTINEL as a score and in the counter
Scanner input = new Scanner (System.in);
do {
System.out.print("Enter your score or ( 99 to quit): ");
newValue = input.nextInt();
counter= counter + 1;
sumOfValues += newValue;
scoresList = new ArrayList();
if(newValue >= 0 && newValue <= 100){
}
else {
System.out.print("Invalid entry: \n");
sumOfValues -= newValue;
counter = counter - 1;
// the program was counting the invalid score
}
}while (newValue != 99);
avg = (double)sumOfValues / counter;
System.out.print("Your average for " + counter + " scores is " + avg+ "\nYour scores are:" + scoresList);
}
}
Your Answer