0

这是我的代码,我以为我输入的所有内容都是正确的:

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
    int[][] userAnswersInt = new int[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[], int userAnswersInt[][]){
    for (int j=0; j<=lines; j++){
    numCorrect[j]=0;
        long[] ara = new long[lines];
        long[] abc = new long[lines];
        ara [j] = Long.parseLong(userAnswers[j]);
    for(int p=0; p<userAnswersInt.length; p++){
        abc [p] = ara[j]%10;
        ara[j] = userAnswersInt[j][p];

    }
    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

但是当我尝试编译并运行它时,它给了我这些错误:

Exception in thread "main" java.lang.NumberFormatException: For input string: "112211324135412"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:495)
at java.lang.Integer.parseInt(Integer.java:527)
at Proj5.gradeSingleQuiz(Proj5.java:52)
at Proj5.main(Proj5.java:27)

我在想也许我没有将该数字的字符串转换为 int 正确但回头看我想我做了,我真的只是不知道这些错误是什么。根据eclipse,在编译之前代码没有任何问题。

我从中提取的文字是这个。

4563123,112211324135412
2312311,222121324135211
2312345,112211324135421
5527687,212111313124412
7867567,111111111111111

第一个数字是学生证,第二个数字是基于数字的答案;T=1 F=2 A=1 B=2 等等。

提前致谢。

编辑:

将我上面的代码更改为 Long 并修复了这些错误,但现在它给了我一个超出范围的异常 5abc [p] = ara[j]%10;我知道这不是我的原始问题,但如果有人能告诉我为什么会这样,我会非常感激,我是不是觉得 5 没有出界?

再次感谢

4

2 回答 2

2

112211324135412不是int number,它显然是 outta int range(-2,147,483,648 and a maximum value of 2,147,483,647 (inclusive)),请尝试使用Long.parseLong(str)

    long[] ara = new long[lines];
    ara [j] = Long.parseLong(userAnswers[j]);
于 2013-03-15T06:05:21.353 回答
0

"112211324135412" => 你可以解析这个字符串,溢出 int。

于 2013-03-15T06:06:50.507 回答