我已经在这个程序上工作了20多个小时,我觉得我真的很接近完成,但我似乎无法修复我的阵列超出范围的例外。我会在这里提供我的全部代码:
import java.util.Scanner;
import java.util.ArrayList;
public class GradeCalcArryas { /*
* Logan Wegner The purpose is to calculate
* entered grades
*/
public static void main(String[] args) {
Scanner s = new Scanner(System.in); // first scanner for inputs
Scanner s1 = new Scanner(System.in); // second scanner for string
boolean done = false; // so an if statement can be inputted for the code
// to break back to the menu
boolean quit = false;
int choice = 0;
final int MAX_STUDENTS = 200;
//Array created to store the information entered for exams
int[] examStats = new int[3];
//Array created to store the information entered for quizzes
int[] quizStats = new int[3];
//Array created to store the information entered for homework
int[] homeworkStats = new int[3];
//Array created to store the student name information entered
String[] studentNames = new String[MAX_STUDENTS];
System.out.println("Welcome to GradeBook!");
System.out.println("Please provide grade item details");
System.out.print("Exams (number, points, weight):");
examStats[0] = s.nextInt(); // inputs exam number
examStats[1] = s.nextInt(); // inputs exam points
examStats[2] = s.nextInt(); // inputs exam weight
System.out.print("Quizzes (number, points, weight):");
quizStats[0] = s.nextInt(); // inputs quiz number
quizStats[1] = s.nextInt(); // inputs quiz points
quizStats[2] = s.nextInt(); // inputs quiz weight
System.out.print("Homework (number, points, weight):");
homeworkStats[0] = s.nextInt(); // inputs homework number
homeworkStats[1] = s.nextInt(); // inputs homework points
homeworkStats[2] = s.nextInt(); // inputs homework weight
/*int numExams = examStats[0];
int numQuizzes = quizStats[0];
int numHW = homeworkStats[0];
int tableLength = numExams + numQuizzes + numHW + 1;*/
/*double[][] examScores = new double[MAX_STUDENTS][];
double[][] quizScores = new double[MAX_STUDENTS][];
double[][] hwScores = new double[MAX_STUDENTS][];*/
//arrays for the average exam, quiz, homework, gradeAverage, and gradeWeightedAverage score of each student
double[] examAverage = new double[MAX_STUDENTS];
double[] quizAverage = new double[MAX_STUDENTS];
double[] hwAverage = new double[MAX_STUDENTS];
double[] gradeAverage = new double[MAX_STUDENTS];
// counters
int numExams = 0;
int numQuizzes = 0;
int numHW = 0;
// declare Double[] exams using length numExams
double[] exams = new double[numExams];
// declare Double[] quizzes using length numQuizzes
double[] quizzes = new double[numQuizzes];
// declare Double[] HW using length numHW
double[] hw = new double[numHW];
//Calculating percentage to multiply exam, quiz, and homework averages
double examWeight = examStats[2]/100;
double quizWeight = quizStats[2]/100;
double hwWeight = homeworkStats[2]/100;
System.out.println("--------------------");
do {
System.out.println("What would you like to do?");
System.out.println(" 1 Add student data");
System.out.println(" 2 Display student grades & statistics");
System.out.println(" 3 Plot grade distribution");
System.out.println(" 4 Quit");
System.out.print("Your choice:");
choice = s.nextInt(); /*
* Choice will determine what the next course of
* action will be with the program
*/
if (choice == 1) { // ADD STUDENT DATA
System.out.println("Enter student data:");
for (int i = 0; i <= MAX_STUDENTS; i++) { // iterate through 200
// times
// (MAX_STUDENTS) or
// break
System.out.print("Data>");
String dataentry = s1.nextLine(); // read inputed data
if (dataentry.equals("done")) { // if user inputs "done",
// break
break;
}
// ArrayList that holds all information (Name, Exams,
// Quizzes, Homework)
ArrayList<String> allInfo = new ArrayList<String>();
// tokenize using ":" delimiter, splitting up the name
// (firstsplit[0]) from the rest of the information
String[] firstsplit = dataentry.split(":");
studentNames[i] = firstsplit[0];
// add name to ArrayList allinfo
allInfo.add(firstsplit[0] + "\t");
// tokenize using " " delimiter, splitting up each score
String[] secondsplit = firstsplit[1].split(" ");
for (int k = 0; k < secondsplit.length; k++) { // iterates
// through
// Array
// secondsplit
allInfo.add(secondsplit[k] + "\t"); // adds item at [k]
// to ArrayList
// allInfo
// if the first char in secondsplit[k] is "e" increment
// numExams
if (secondsplit[k].subSequence(0, 1).equals("e"))
numExams++;
// if the first char in secondsplit[k] is "q" increment
// numQuizzes
if (secondsplit[k].subSequence(0, 1).equals("q"))
numQuizzes++;
// if the first char in secondsplit[k] is "h" increment
// numHW
if (secondsplit[k].subSequence(0, 1).equals("h"))
numHW++;
}
// iterates through Array exams and adds values from allInfo
for (int k = 0; k < exams.length; k++) {
exams[k] = Double.parseDouble(allInfo.get(1 + k)
.substring(1));
}
// iterates through Array quizzes and adds values from
// allInfo
for (int k = 0; k < quizzes.length; k++) {
quizzes[k] = Double.parseDouble(allInfo.get(
1 + numExams + k).substring(1));
}
// iterates through Array hw and adds values from allInfo
for (int k = 0; k < hw.length; k++) {
hw[k] = Double.parseDouble(allInfo.get(
1 + numExams + numQuizzes + k).substring(1));
}
//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++;
}
}
}
//This choice is to display student grades & statistics in a table
if (choice == 2) {
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++;
}
}
if (choice == 3) {
}
if (choice == 4) {
quit = true;
System.out.println("Good bye!");
}
}while (quit == false);
}
}
我的出界异常发生在这里:
//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++;
}
我的例外是ArrayOutofBounds:0或ArrayOutofBounds:3。它与我的考试,测验和hw数组有关。我已经在我的程序中移动它们并更改了我的numExams,numQuizzes和numHW值,但它仍然给我带来了一些麻烦。我喜欢一些见解。先谢谢你们。
答案 0 :(得分:0)
肯定你有问题:
int numExams = 0;
int numQuizzes = 0;
int numHW = 0;
// declare Double[] exams using length numExams
double[] exams = new double[numExams];
// declare Double[] quizzes using length numQuizzes
double[] quizzes = new double[numQuizzes];
// declare Double[] HW using length numHW
double[] hw = new double[numHW];
你在这里声明大小为0的数组。如果你有更多问题 - 我没有检查。