当我的程序到达这个 for 循环并尝试运行它时,我会收到一个错误
for (int m = 0; m < students; m++){
allScores = allScores + scores[m];
学生被声明为 int allScores 被声明为双精度,分数 [] 数组也是如此。
这是错误消息:
线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 2 在 Lab4.main(Lab4.java:51)
第 51 行是 for (int m = 0; m < students; m++) 行
编辑:
这是我的全部代码,我可能没有正确完成 score[] 数组:
import java.util.*;
import java.text.*;
public class Lab4 {
public static void main(String[] args){
Scanner s= new Scanner(System.in);
String input;
int students;
int d=1;
double allScores = 0;
char [] answerKey= { 'B' , 'D' , 'A' , 'A' , 'C' , 'A' , 'B' , 'A' , 'C' , 'D' , 'B' , 'A' };
char [] userAnswers = new char[answerKey.length];
DecimalFormat df = new DecimalFormat("#0.0");
System.out.print("how many students are in the class? ");
input = s.nextLine();
students=Integer.parseInt(input);
String [] name = new String[students];
double [] scores = new double[students];
for (int j = 0; j < students; ++j)
{
int correctAnswers=0;
System.out.print("Enter name of student " + (j + 1) + ": ");
name[j] = s.nextLine();
System.out.print("Enter quiz score answers :");
String line = s.nextLine().toUpperCase();
for (int k = 0; k < answerKey.length; k++) {
userAnswers[k] = line.charAt(k);
}
for (int i = 0; i < userAnswers.length; i++)
{
if(userAnswers[i] == answerKey[i])
{
correctAnswers++;
}
}
System.out.println(name[j]);
System.out.println((df.format((((float)(correctAnswers) / answerKey.length)) * 100)) + "%");
scores [d] = ((float)((correctAnswers) / answerKey.length) * 100);
d++;
}
System.out.println("the high score is " + "and the low score is " );
for (int m = 0; m < scores.length; m++){
allScores = allScores + scores[m];
}
System.out.println("Average is:" + ((allScores/students)));
}
}
编辑:
对不起,我读错了第 51 行是
scores [d] = ((float)((correctAnswers) / answerKey.length) * 100);
编辑:
新代码不再给我错误消息,但我似乎无法让分数的平均值起作用。
import java.util.*;
import java.text.*;
public class Lab4 {
public static void main(String[] args){
Scanner s= new Scanner(System.in);
String input;
int students;
int d=1;
double allScores = 0;
char [] answerKey= { 'B' , 'D' , 'A' , 'A' , 'C' , 'A' , 'B' , 'A' , 'C' , 'D' , 'B' , 'A' };
char [] userAnswers = new char[answerKey.length];
DecimalFormat df = new DecimalFormat("#0.0");
System.out.print("how many students are in the class? ");
input = s.nextLine();
students=Integer.parseInt(input);
String [] name = new String[students];
double [] scores = new double[students + 1];
for (int j = 0; j < students; ++j)
{
int correctAnswers=0;
System.out.print("Enter name of student " + (j + 1) + ": ");
name[j] = s.nextLine();
System.out.print("Enter quiz score answers :");
String line = s.nextLine().toUpperCase();
for (int k = 0; k < answerKey.length; k++) {
userAnswers[k] = line.charAt(k);
}
for (int i = 0; i < userAnswers.length; i++)
{
if(userAnswers[i] == answerKey[i])
{
correctAnswers++;
}
}
System.out.println(name[j]);
System.out.println((df.format((((float)(correctAnswers) / answerKey.length)) * 100)) + "%");
scores [d] = ((float)((correctAnswers) / answerKey.length) * 100);
d++;
}
System.out.println("the high score is " + "and the low score is " );
for (int m = 0; m < scores.length; m++){
allScores = allScores + scores[m];
}
System.out.println("Average is:" + ((allScores/students)));
}
}