计算等级平均值,编程不断出错

时间:2014-11-30 04:52:27

标签: java arrays

我对此代码的工作是计算成绩和最终加权平均成绩,并将其输出到表格中。我在这方面遇到了麻烦,因为我不断得到一个数组出界的地方:

examAverage[index] = ...etc

我的MAX_STUDENTS是200,因为这是可以将成绩存储在我的课程中的学生数量最多的学生。请帮我弄清楚我需要改变哪些变量才能使这个程序运行良好。如果我需要提供更多信息,请告诉我。这是我的其余代码:

if (choice == 2) {

            //Index counters for averages
            int examIndex = 0;
            int quizIndex = 0;
            int hwIndex = 0;
            int gradeAveragingIndex = 0;

            //loop finding the gradeAverage
            for(int index = 0; index<=MAX_STUDENTS; index++){
                examAverage[index] = (exams[examIndex] + exams[examIndex+1] + exams[examIndex+2]) / numExams;
                quizAverage[index] = (quizzes[quizIndex] + quizzes[quizIndex+1] + quizzes[quizIndex+2]) / numQuizzes;
                hwAverage[index] = (hw[hwIndex] + hw[hwIndex+1] + hw[hwIndex+2]) / numHW;
                gradeAverage[index] = ((examAverage[gradeAveragingIndex] * examWeight) + (quizAverage[gradeAveragingIndex] * quizWeight) + (hwAverage[gradeAveragingIndex] * hwWeight) / numExams);

                examIndex+=3;
                quizIndex+=3;
                hwIndex+=3;
                gradeAveragingIndex++;

            }

            System.out.println("Display student grades & statistics");

            //Formatting for the heading of my grade table
            System.out.printf("%-10s","Name");
            System.out.printf("%-5s","Exam");
            System.out.printf("%-5s","Exam");
            System.out.printf("%-5s","Exam");
            System.out.printf("%-5s","Quiz");
            System.out.printf("%-5s","Quiz");
            System.out.printf("%-5s","Quiz");
            System.out.printf("%-7s","HWork");
            System.out.printf("%-7s","HWork");
            System.out.printf("%-7s","HWork");
            System.out.printf("%-5s","Grade\n");

            //declaring index counters
            int studentNameIndex = 0;
            int examGradeIndex = 0;
            int quizGradeIndex = 0;
            int homeworkGradeIndex = 0;
            int gradeAverageIndex = 0;

            //for loop for the output of exams, quizzes, homework, and grade average
            for(int index = 0; index < studentNames.length; index++) {
                System.out.printf("%-10s",studentNames[studentNameIndex]);
                System.out.printf("%-5.1f",exams[examGradeIndex]);
                System.out.printf("%-5.1f",exams[examGradeIndex+1]);
                System.out.printf("%-5.1f",exams[examGradeIndex+2]);
                System.out.printf("%-5.1f",quizzes[quizGradeIndex]);
                System.out.printf("%-5.1f",quizzes[quizGradeIndex+1]);
                System.out.printf("%-5.1f",quizzes[quizGradeIndex+2]);
                System.out.printf("%-7.1f",hw[homeworkGradeIndex]);
                System.out.printf("%-7.1f",hw[homeworkGradeIndex+1]);
                System.out.printf("%-7.1f",hw[homeworkGradeIndex+2]);
                System.out.printf("%-5.1f",gradeAverage[gradeAverageIndex] + "\n");

                studentNameIndex++;
                examGradeIndex+=3;
                quizGradeIndex+=3;
                homeworkGradeIndex+=3;
                gradeAverageIndex++;
            }

                }

1 个答案:

答案 0 :(得分:0)

这一行:

for(int index = 0; index<=MAX_STUDENTS; index++){

应该是:

for(int index = 0; index<MAX_STUDENTS; index++){

只需将<=更改为<

现在您的程序正在尝试访问200个元素数组的第200个元素。 200元素数组的索引从0到199,这意味着200超出范围。你需要从0到199,这种改变就是这样。