A. Moore A. Moore - 1 month ago 16
Java Question

Struggling to exclude certain numbers from my count?

My programs purpose is to calculate the average, maximum, and minimum of the test scores entered by a user.

Here is the problem I keep running into: My program is built to deny test scores entered that are either below zero or above 100, which it does, but for example, if the user inputed 108, it would say "ERROR: Not a valid test score. " BUT even when the score is denied, it still adds that score to the count, and 108 would be the maximum. If an invalid score is entered, I need for it to be completely excluded from the program.
Can anyone explain how to fix this?

Here is my code.

import java.util.Scanner;
import java.io.*;
import java.text.DecimalFormat;

public class hw {

public static void main(String[] args) {

int maxGrade = Integer.MIN_VALUE;
int minGrade = Integer.MAX_VALUE;
int count = 0;
int total = 0;
final int SENTINEL = -1;
int score;

Scanner scan = new Scanner(System.in);

System.out.println("To calculate the class average, enter each test
score.");
System.out.println("When you are finished, enter a -1.");

System.out.print("Enter the first test score > ");
score = scan.nextInt();


while (score != SENTINEL)
{
total += score;
//add one to the count
count++;
//calculate the maximum and the minimum
if (score > maxGrade) {
maxGrade = score;
}
if (score < minGrade) {
minGrade = score;
}

if (score < 0 || score> 100)
System.out.print("ERROR: Not a valid test score. ");

System.out.println(" Enter the next test score > ");
score = scan.nextInt();

}
if (count != 0) {
DecimalFormat oneDecimalPlace = new DecimalFormat("0.0");

System.out.println("\nThe class average is "
+ oneDecimalPlace.format((double) (total) / count));
System.out.println("The minimum value is " + minGrade);
System.out.println("The maximum value is " + maxGrade);
} else {
System.out.println("\nNo grades were entered");
}

}
}

Answer

You have to add to total, calculate max and min only if 0 < input < 100

import java.util.Scanner;
import java.io.*;
import java.text.DecimalFormat;

public class hw {

public static void main(String[] args) {

    int maxGrade = Integer.MIN_VALUE;
    int minGrade = Integer.MAX_VALUE;
    int count = 0;
    int total = 0;
    final int SENTINEL = -1;
    int score;

    Scanner scan = new Scanner(System.in);

    System.out.println("To calculate the class average, enter each test score.");
    System.out.println("When you are finished, enter a -1.");

    System.out.print("Enter the first test score > ");
    score = scan.nextInt();


    while (score != SENTINEL) 
    {

        if (score < 0 || score> 100)
        {
            System.out.print("ERROR: Not a valid test score. ");
        }
        else
        {
            //add one to the count
            count++;
            //calculate the maximum and the minimum
            if (score > maxGrade) {
                maxGrade = score;
            }
            if (score < minGrade) {
                minGrade = score;
            }
            total += score;
        }
        System.out.println(" Enter the next test score > ");
        score = scan.nextInt();

    }
    if (count != 0) {
        DecimalFormat oneDecimalPlace = new DecimalFormat("0.0");

        System.out.println("\nThe class average is "
                + oneDecimalPlace.format((double) (total) / count));
        System.out.println("The minimum value is " + minGrade);
        System.out.println("The maximum value is " + maxGrade);
    } else {
        System.out.println("\nNo grades were entered");
    }

}
}