当程序到达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&lt; students; m ++)line
编辑:
这是我的整个代码,我可能没有正确完成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)));
}
}
答案 0 :(得分:4)
问题是students
被初始化为大于scores.length
的值。您可以使用scores.length
代替students
作为循环的上限;或者,将students
初始化为scores.length
(或低于它的东西)。
如果您只想迭代整个数组,可以使用enhanced for loop语法:
for (double score : scores) {
allScores = allScores + score;
}
编辑我自己计算了一些线条(有点单调乏味),我就登上了这条线:
scores [d] = ((float)((correctAnswers) / answerKey.length) * 100);
发生的事情是d
太大了。这可能是因为您将d
初始化为1而不是0。
答案 1 :(得分:3)
在数组中使用length属性进行迭代。
for (int m = 0; m < scores.length; m++){
allScores = allScores + scores[m];
但是,您提到错误发生在第51行,但第51行是:
scores [d] = ((float)((correctAnswers) / answerKey.length) * 100);
所以你的d等于或大于scores.length。
我很好奇你为什么用1?
初始化d将其更改为0可能会解决问题。
int d=0;
代码中的另一个问题是:
((float)((correctAnswers) / answerKey.length) * 100);
将其更改为
(((float)correctAnswers / answerKey.length) * 100);
否则,您更改结果,该结果已经是浮点数的int。这将影响结果。
答案 2 :(得分:0)
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:
这是因为您访问超出限制的数组。可能在scores[m];
您可以使用以下内容:
for (int m = 0; m < scores.length; m++){
allScores = allScores + scores[m];
答案 3 :(得分:-1)
您的代码存在一些不符之处:
int d=1; // your score[index] starts with this just change to 0;
scores [d] = ((float)((correctAnswers) / answerKey.length) * 100);
//your scores length will now be d + students; use the students instead
for (int m = 0; m < scores.length; m++){ //for (int m=0;m<students;m++)
allScores = allScores + scores[m];
}