import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
double score = 0, avg, sum = 0;
int index = 0, count = 0, num = 0;
num = readInput(keyboard, num);
double Test[] = new double[num];
System.out.print("\n");
while(count < Test.length)
{
System.out.printf("Enter your test score #%d:", count + 1);
try
{
score = keyboard.nextDouble();
}
catch(java.util.InputMismatchException e)
{
System.out.print("You have entered a non-numerical value, please try again\n");
keyboard.next();
continue;
}
if (score >= 0)
{
Test[count] = score;
sum = sum + score;
}
else
{
System.out.print("You have entered an invalid grade, please try again\n");
continue;
}
count++;
}
double maxValue = Test[0];
for (int i = 1; i < Test.length; i++)
{
if (Test[i] > maxValue)
{
maxValue = Test[i];
}
}
double minValue = Test[0];
for (int i = 1; i < Test.length; i++)
{
if (Test[i] < minValue)
{
minValue = Test[i];
index = i;
}
}
System.out.print("\nThe highest value is: " + maxValue + "\n");
System.out.print("The lowest value is: " + minValue + "\n");
avg = sum / Test.length;
System.out.printf("Your grade average is: %.1f \n\n", avg);
System.out.print("Would you like to drop the lowest grade: Y or N:");
char choice = keyboard.next().charAt(0);
if (choice == 'Y' || choice == 'y')
{
double newSum = sum - minValue;
double newAvg = newSum / (Test.length - 1);
System.out.print("\nYour old scores were: ");
System.out.print(Test[0]);
for (int i = 1; i < Test.length; i++)
{
System.out.print(", " + Test[i]);
}
System.out.print("\n\nYour new scores are: ");
for (int i = 0; i < Test.length; i++)
{
if (index != 0 && i != index)
{
if (i >= 1)
{
System.out.print(", ");
}
System.out.print(Test[i]);
}
else if (i != index)
{
if (i >= 2)
{
System.out.print(", ");
}
System.out.print(Test[i]);
}
}
System.out.printf("\n\nYour new average is: %.1f\n", newAvg);
}
else if (choice == 'N' || choice == 'n')
{
System.out.print("Your average has stayed the same.\n");
return;
}
else
{
System.out.print("You have entered an invalid response. Bye.\n");
return;
}
}
public static int readInput(Scanner keyboard, int val)
{
System.out.print("\nHow many scores would you like to enter:");
val = 0;
try
{
val = keyboard.nextInt();
}
catch(java.util.InputMismatchException e)
{
System.out.print("You have entered a non-numerical value.\n");
keyboard.next();
readInput(keyboard, val);
}
if (val < 0)
{
System.out.print("You have entered a negative value.\n");
readInput(keyboard, val);
}
else if (val == 0)
{
System.out.print("If you have no test grades then you do not need this program.\n");
readInput(keyboard, val);
}
return val;
}
}
错误是由代码底部的函数引起的。
这是我目前的输出:
您想要输入多少分数:h 您输入了非数值。
您想要输入多少分数:8 如果您没有测试成绩,那么您不需要此程序。
您想要输入多少分数:8
线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:0 在Main.main(Main.java:53) 以非零状态退出
似乎无法识别输入的第二个值。即使我进入8,该程序认为它为零,我无法弄清楚原因。在无效输入后的第三个条目上程序崩溃。注意:如果用户在第一次尝试时输入有效值,则程序正常工作。只有当用户首先输入无效输入时才会发生此错误。
编辑:我知道错误是因为我的数组中没有值,但我在问为什么会发生这种情况。输入无效输入后,该值不会从函数返回,因此数组在空时尝试运行。