문제

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
도움이 되었습니까?

해결책

You're almost there... in your if you're ensuring that all scores are within the proper range.

When the if fails, you want to throw the IllegalArgumentException in an else, like this:

if (scores[0] >= 0 && scores[0] <= 100 || 
    scores[1] >= 0 && scores[1] <= 100 ||
    scores[2] >= 0 && scores[2] <= 100)
{
    average = totalScores / 3;        
    System.out.println("Average Score: " + average);
}
else 
{
   throw new IllegalArgumentException("Numbers were too low or high.");
}

다른 팁

The syntax is

if (condition) {
    throw new IllegalArgumentException("message here");
}

It can catch an exception that the app throw an exception in the block of try.

In your block of try, we just see average = totalScores / 3;, it don't throw any exception. So it don't catch anything what was throwed.

You can use this function to throw an exception - IllegalArgumentException.

public static int getInputScore(Scanner keyboard) {
    int score = keyboard.nextInt();
    if (score < 0 || score >= 100) {
        throw new IllegalArgumentException(); 
    }
    return score;
}

And use it in main code:

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);

    int[] scores = new int[3];

    System.out.println("Score 1:");
    try {
        scores[0] = getInputScore(keyboard);
    } catch (IllegalArgumentException e) {
        System.out.println("Numbers were too low or high.");
        return;
    }

    System.out.println("Score 2:");
    try {
        scores[1] = getInputScore(keyboard);
    } catch (IllegalArgumentException e) {
        System.out.println("Numbers were too low or high.");
        return;
    }

    System.out.println("Score 3:");
    try {
        scores[2] = getInputScore(keyboard);
    } catch (IllegalArgumentException e) {
        System.out.println("Numbers were too low or high.");
        return;
    }

    int totalScores = scores[0] + scores[1] + scores[2];
    int average = totalScores / 3;
    System.out.println("Average Score: " + average);
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top