为什么每次输入都会出现 IllegalArgumentException?
Why am i getting an IllegalArgumentException for every input?
我需要创建一个程序来计算用户输入的测试分数的平均值。这实际上是我第二次接到任务要求制作这个程序。但是这次作业要我在用户输入小于 0 或大于 100 的测试分数时抛出 IllgalArgumentException。问题是无论测试分数是多少,即使测试分数在 0-100 之间,程序也会因 IllegalArgumentException 而崩溃.不确定我在做什么错任何想法这里的预期输出无一例外
预期输出
Enter number of test scores:5
Enter test score 1:70
Enter test score 2:65
Enter test score 3:94
Enter test score 4:55
Enter test score 5:90
74.8
和我的代码
public static void main(String[] args) throws IllegalArgumentException
{
Scanner input = new Scanner (System.in);
int numTests = 0;
int [] grade = new int [numTests];
double average;
double totalGrades = 0;
System.out.print("Enter number of test scores:");
numTests = input.nextInt();
grade = new int[numTests];
for (int index = 0; index < grade.length; index++)
{
System.out.print("Enter test score " + (index + 1) + ":");
grade[index] = input.nextInt();
if(grade[index] < 0 || grade[index] > 100);
{
throw new IllegalArgumentException();
}
}
for (int i = 0; i < grade.length; i++)
{
totalGrades += grade[i];
}
average = totalGrades / grade.length;
System.out.println(average);
}
private static class IllegalArgumentException extends Exception
{
public IllegalArgumentException()
{
super("Test scores must have a value less than 100 and greater than 0.");
}
public IllegalArgumentException(int value)
{
super("Invalid Test Score: " + value);
}
}
if(grade[index] < 0 || grade[index] > 100);
{
throw new IllegalArgumentException();
}
if
行末尾的 ;
表示 throw
无条件执行,因为它不在条件块中。
您的代码(实际上)与:
if(grade[index] < 0 || grade[index] > 100) { /* do nothing */ }
throw new IllegalArgumentException();
删除 ;
.
我需要创建一个程序来计算用户输入的测试分数的平均值。这实际上是我第二次接到任务要求制作这个程序。但是这次作业要我在用户输入小于 0 或大于 100 的测试分数时抛出 IllgalArgumentException。问题是无论测试分数是多少,即使测试分数在 0-100 之间,程序也会因 IllegalArgumentException 而崩溃.不确定我在做什么错任何想法这里的预期输出无一例外
预期输出
Enter number of test scores:5
Enter test score 1:70
Enter test score 2:65
Enter test score 3:94
Enter test score 4:55
Enter test score 5:90
74.8
和我的代码
public static void main(String[] args) throws IllegalArgumentException
{
Scanner input = new Scanner (System.in);
int numTests = 0;
int [] grade = new int [numTests];
double average;
double totalGrades = 0;
System.out.print("Enter number of test scores:");
numTests = input.nextInt();
grade = new int[numTests];
for (int index = 0; index < grade.length; index++)
{
System.out.print("Enter test score " + (index + 1) + ":");
grade[index] = input.nextInt();
if(grade[index] < 0 || grade[index] > 100);
{
throw new IllegalArgumentException();
}
}
for (int i = 0; i < grade.length; i++)
{
totalGrades += grade[i];
}
average = totalGrades / grade.length;
System.out.println(average);
}
private static class IllegalArgumentException extends Exception
{
public IllegalArgumentException()
{
super("Test scores must have a value less than 100 and greater than 0.");
}
public IllegalArgumentException(int value)
{
super("Invalid Test Score: " + value);
}
}
if(grade[index] < 0 || grade[index] > 100);
{
throw new IllegalArgumentException();
}
if
行末尾的 ;
表示 throw
无条件执行,因为它不在条件块中。
您的代码(实际上)与:
if(grade[index] < 0 || grade[index] > 100) { /* do nothing */ }
throw new IllegalArgumentException();
删除 ;
.