这是我的代码,除了一个错误外,我几乎已经完成了所有的工作:
import java.util.*;
import java.io.*;
public class Proj5 {
public static void main(String[] args)throws IOException{
Scanner s = new Scanner(System.in);
int [] quizKey = {1,1,2,2,1,1,3,2,4,1,3,5,4,1,2};
String [] userAnswers = new String[100];
String [] wid = new String[100];
int [][] userIndividualAnswers = new int[quizKey.length][userAnswers.length];
int [] numCorrect = new int[quizKey.length];
int max;
int min;
int lines=0;
readInText();
s = readInText();
while(s.hasNext()){
String line = s.nextLine();
String[] tokens = line.split(",");
wid[lines] = tokens[0];
userAnswers[lines] = tokens[1];
lines ++;
}// end while loop
Long[][] userAnswersInt = new Long[quizKey.length][lines];
numCorrect = gradeSingleQuiz(lines, quizKey, userAnswers, numCorrect, userAnswersInt);
double[] percentCorrect = new double[lines];
percentCorrect = percentCorrect(lines, numCorrect, quizKey);
char[] grades = new char[lines];
grades = grade(numCorrect, lines);
displayOutput(wid, lines, numCorrect, grades, percentCorrect);
}//end main
public static Scanner readInText()throws IOException{
Scanner inFile = new Scanner(new File("QuizScores.txt"));
return inFile;
}// end readInText
public static String[] userAnswers(String userAnswers[]){
return userAnswers;
}
public static int[] gradeSingleQuiz(int lines, int quizKey[], String userAnswers[], int numCorrect[], Long userAnswersInt[][]){
for (int j=0; j<lines; j++){
numCorrect[j]=0;
long[] ara = new long[userAnswers.length];
long[] abc = new long[userAnswers.length];
ara [j] = Long.parseLong(userAnswers[j]);
for(int p=0; p<ara.length; p++){
abc [p] = ara[j]%10;
userAnswersInt[p][j] = ara[j]/10;
}
for(int n=0; n<=quizKey.length; n++){
if(userAnswersInt[j][n]==(quizKey[n])){
numCorrect[j]++;
}
}
}//end for loop
return numCorrect;
}// end gradeSingleQuiz
public static int max(int max, int numCorrect[]){
max = numCorrect[0];
for(int r=1; r<numCorrect.length; r++){
if(numCorrect[r]>max){
max=numCorrect[r];
}
}
return max;
}
public static int min(int min, int numCorrect[]){
min = numCorrect[0];
for(int r=1; r<numCorrect.length; r++){
if(numCorrect[r]<min){
min=numCorrect[r];
}
}
return min;
}
public static char[] grade(int numCorrect[], int lines){
char[] grade = new char[lines];
for (int j=0; j<=lines; j++){
if(numCorrect[j]>=14)
grade[j]='A';
else if((numCorrect[j]>=12)&&(numCorrect[j]<14))
grade[j]='B';
else if((numCorrect[j]>=11)&&(numCorrect[j]<12))
grade[j]='C';
else if ((numCorrect[j]>=9)&&(numCorrect[j]<11))
grade[j]='D';
else
grade[j]='F';
}
return grade;
}//end grade
public static double[] percentCorrect(int lines, int numCorrect[], int quizKey[]){
double[] centCorrect = new double[100];
for (int j=0; j<=lines; j++){
centCorrect[j] = numCorrect[j]/quizKey.length;
}
return centCorrect;
}
public static void averageScore(int lines, double percentCorrect[]){
double add=0;
for(int d=0; d<=lines; d++){
add = percentCorrect[d] + add;
}//end for loop
System.out.println("Average: " + add + "%");
}// end averageScore
public static void displayOutput(String wid[], int lines, int numCorrect[], char grades[], double percentCorrect[]){
System.out.println("Student ID # Correct %Correct Grade");
for(int i=0; i<lines; i++){
System.out.println(wid[0] + " " + numCorrect[i] + " " +
(percentCorrect[i]) + " " + grades[i]);
}
}// end display output
}//end class
当我尝试编译它时,它给我这个错误,我正在使用eclipse:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 15
at Proj5.gradeSingleQuiz(Proj5.java:55)
at Proj5.main(Proj5.java:27)
有问题的一行
userAnswersInt[p][j] = ara[j]/10;
我似乎无法弄清楚自己,有什么我没有在这里看到的,或者我只是完全以错误的方式解决这个问题?
提前致谢
编辑:
来自txt文件的txt是:
4563123,112211324135412
2312311,222121324135211
2312345,112211324135421
5527687,212111313124412
7867567,111111111111111
答案 0 :(得分:1)
在gradeSingleQuiz()方法中,数组ara
的初始化大小为userAnswersArray
,但是从j = 0到j = lines-1循环。数组userAnswersArray
的大小始终为100,但lines
的长度取决于控制台的输入。
答案 1 :(得分:0)
我检查了你的逻辑,发现了以下逻辑错误
您将数组ara分配给长度为100,然后尝试循环一个只有15个元素长的数组对象
long [] ara = new long [userAnswers.length];
下面的行返回null,因此抛出空指针异常。
如果(userAnswersInt [j]的[N] ==(quizKey [N]))
我建议您重新审视逻辑,如果需要帮助,请在此处发布要求,可以帮助解决逻辑问题。