这是我发布的第一个问题。我用这个网站查看了其他问题,以帮助解决我遇到的类似问题。经过一番粗略的查找后,我找不到我正在寻找的答案,所以我决定最终屈服并创建一个帐户。
我对 java 还很陌生,刚上第一堂课几周。无论如何,我的项目是创建一个程序,它可以接收任意数量的学生和他们的成绩,然后给他们分配一个字母等级。然而,问题在于它是在一种曲线上,而其他等级的字母取决于最高等级。任何等于或低于最好成绩的 10 分都是 a,任何低于 11-20 分的都是 ab,依此类推。我要使用数组,但运行时出现此错误“ Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException.
”
我将继续在下面发布我的代码。感谢您提供的任何建议。
package grade.calculator;
import java.util.Scanner;
/**
*
* @author nichol57
*/
public class GradeCalculator {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of students");
int number = input.nextInt();
double[]grades = new double [number];
for (int J = number; J >=0; J--) {
System.out.println("Enter the students' grades");
grades[J] = input.nextDouble();
}
double best = grades[0];
for (int J = 1; J < number; J++) {
if (grades[J] >= best){
best = grades[J];
}
}
for (int J = 0;J < number; J++){
if (grades[J] >= best - 10){
System.out.println("Student " + J + " score is " + grades[J] +
" and grade is " + "A");
}
else if (grades[J] >= best - 20){
System.out.println("Student " + J + " score is " + grades[J] +
" and grade is " + "B");
}
else if (grades[J] >= best - 30) {
System.out.println("Student " + J + " score is " + grades[J] +
" and grade is " + "C");
}
else if (grades[J] >= best - 40) {
System.out.println("Student " + J + " score is " + grades[J] +
" and grade is " + "D");
}
else {
System.out.println("Student " + J + " score is " + grades[J] +
" and grade is " + "F");
}
} // end for loop for output
}// end main method
}