I need the program to accept 3 test scores and then print their average, but if a score is less than -1 or greater than 100, it should throw an IllegalArgumentException. I can get the average to print out, but when testing -1 or 101, it doesn't throw the exception. What am I doing wrong?
I'm very new to learning exceptions, so any help is appreciated.
Here's my code:
import java.util.Scanner;
import java.io.*;
public class TestScores
{
public static void main(String[]args)
{
Scanner keyboard = new Scanner(System.in);
int[]scores = new int [3];
System.out.println("Score 1:");
scores[0] = keyboard.nextInt();
System.out.println("Score 2:");
scores[1] = keyboard.nextInt();
System.out.println("Score 3:");
scores[2] = keyboard.nextInt();
int totalScores = scores[0] + scores[1] + scores[2];
int average = 0;
if (scores[0] >= 0 && scores[0] <= 100 ||
scores[1] >= 0 && scores[1] <= 100 ||
scores[2] >= 0 && scores[2] <= 100)
{
try
{
average = totalScores / 3;
}
catch(IllegalArgumentException e)
{
System.out.println("Numbers were too low or high.");
}
System.out.println("Average Score: " + average);
}
} //end of public static void
} //end of TestScores