0

当我的程序到达这个 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)));

}

}
4

4 回答 4

4

问题在于,它students被初始化为一个大于 的值scores.length。您可以使用scores.length而不是students作为循环的上限;或者,初始化studentsscores.length(或低于它的东西)。

如果只想遍历整个数组,可以使用增强的 for 循环语法:

for (double score : scores) {
    allScores = allScores + score;
}

编辑我自己数了几行(有点乏味),然后我落在了这条线上:

scores [d] = ((float)((correctAnswers) / answerKey.length) * 100);

正在发生的事情是d变得太大了。这可能是因为您初始化d为 1 而不是 0。

于 2013-02-27T04:17:49.000 回答
3

使用数组中的长度属性进行迭代。

for (int m = 0; m < scores.length; m++){
        allScores = allScores + scores[m];

但是,您提到错误发生在第 51 行,但第 51 行是:

scores [d] = ((float)((correctAnswers) / answerKey.length) * 100);

所以你的 d 等于或大于 score.length。

我很好奇你为什么用 1 初始化 d?

将其更改为 0 可能会解决问题。

int d=0;

您的代码中的另一个问题是:

((float)((correctAnswers) / answerKey.length) * 100);

将其更改为

(((float)correctAnswers / answerKey.length) * 100);

否则,您将更改结果,该结果已经是要浮动的 int。这会影响结果。

于 2013-02-27T04:17:58.597 回答
0
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:  

这是因为您在某些地方访问数组超出了其限制。可能在scores[m];

您可以使用以下内容:

for (int m = 0; m < scores.length; m++){
        allScores = allScores + scores[m];  
于 2013-02-27T04:21:02.023 回答
-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];
    }
于 2013-02-27T04:38:21.730 回答