0

我正在尝试做的只是反转每行中的数字,因此 row[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
    Long[][] userAnswersInt = new Long[lines][quizKey.length];
    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);
    max=max(numCorrect);
    min=min(numCorrect);

    displayOutput(wid, lines, numCorrect, grades, percentCorrect);
    averageScore(max, min, lines, 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[quizKey.length];
        ara [j] = Long.parseLong(userAnswers[j]);
        for(int p=0; p<ara.length; p++){
            System.out.println(p);
            userAnswersInt[j][p]=ara[j]%10;
            ara[j]=ara[j]/10;
        }
        int rows = userAnswersInt.length;
        int cols = userAnswersInt[0].length;
        long[][] userAnswersReverse = new long[rows][cols];
        for(int q=rows-1; q>=0;q--){
            for(int w = cols-1; w >=0; w++){
                userAnswersReverse[rows-1-q][cols-1-q] = userAnswersInt[q][w];

System.out.print(userAnswersReverse[q][w]);
            }
        }
        for(int n=0; n<quizKey.length; n++){
            System.out.println(userAnswersInt[j][n]);
            if(userAnswersInt[j][n].equals(quizKey[n])){
                    numCorrect[j]++;    
            }
        }
    }//end for loop

return numCorrect;
}// end gradeSingleQuiz

public static int max(int numCorrect[]){
    int 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 numCorrect[]){
    int 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 min, int max, 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 + "%");
    System.out.println("High Score: " + max);
    System.out.println("Low Score: " + min);
}// 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[i] + "          " + numCorrect[i] + "          " +
                (percentCorrect[i]) + "%" + "       " + grades[i]); 
    }

}// end display output

}//end class

我的重点是:

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[quizKey.length];
        ara [j] = Long.parseLong(userAnswers[j]);
        for(int p=0; p<ara.length; p++){
            System.out.println(p);

userAnswersInt[j][p]=ara[j]%10;
            ara[j]=ara[j]/10;
        }
        int rows = userAnswersInt.length;
        int cols = userAnswersInt[0].length;
        long[][] userAnswersReverse = new long[rows][cols];
        for(int q=rows-1; q>=0;q--){
            for(int w = cols-1; w >=0; w++){
                userAnswersReverse[rows-1-q][cols-1-q] = userAnswersInt[q][w];
                System.out.print(userAnswersReverse[q][w]);
            }
        }
        for(int n=0; n<quizKey.length; n++){
            System.out.println(userAnswersInt[j][n]);
            if(userAnswersReverse[j][n]==(quizKey[n])){
                    numCorrect[j]++;    
            }
        }
    }//end for loop

return numCorrect;
}// end gradeSingleQuiz

它给我的错误是:(我正在使用eclipse)

Exception in thread "main" java.lang.NullPointerException
at Proj5.gradeSingleQuiz(Proj5.java:65)
at Proj5.main(Proj5.java:27)

第 65 行是:

userAnswersReverse[rows-1-q][cols-1-q] = userAnswersInt[q][w];

希望这是足够的信息让你们帮助我,并提前感谢,如果我需要包括其他任何内容,请告诉我。

编辑:

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[quizKey.length];
        ara [j] = Long.parseLong(userAnswers[j]);
        for(int p=0; p<ara.length; p++){
            System.out.println(p);
            userAnswersInt[j][p]=ara[j]%10;
            ara[j]=ara[j]/10;
        }
        int rows = userAnswersInt.length;
            int cols = userAnswersInt[0].length;
            long[][] userAnswersReverse = new long[rows][cols];
        if(j==4){
            for(int q=0; q<userAnswersInt.length;q++){
            for(int w = cols-1; w >=0; w--){
                userAnswersReverse[q][cols-1-w] = userAnswersInt[q][w];
                System.out.print(userAnswersReverse[q][w]);
                }
            }
        }

if(j==4){
            for(int r=0; r<lines; r++){
                for(int n=0; n<quizKey.length; n++){
                    System.out.println(userAnswersReverse[r][n]);
                    if(userAnswersReverse[r][n]==(quizKey[n])){
                    numCorrect[r]++;    

}
                }
            }
        }   
    }//end for loop

return numCorrect;
}// end gradeSingleQuiz

现在的问题是我的值没有得到正确返回知道为什么吗?这就是我现在得到的:

Student ID    # Correct    %Correct    Grade
 4563123          15          1.0%       A
 2312311          9          0.0%       D
 2312345          13          0.0%       B
 5527687          9          0.0%       D
 7867567          6          0.0%       F
Average: 1.0%
High Score: 0
Low Score: 15

我从中提取的文件是:

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

1 回答 1

0

你这里有几个错误。

导致 NullPointerException 的原因是您正在创建一个 Long[][] 类型的数组。默认情况下,此处的条目将为空。如果它是 "long[][]" - 小 'l' - 它们将是 0。这不会导致任何异常,但它可能仍然没有做你想做的事。

数组中的某些条目在函数的第 6-11 行初始化,但仅在第一行初始化,然后在导致问题的行上使用其中一个未初始化的空值。

也许循环结构意味着不同,以便在开始反转之前将第 6-11 行的代码应用于所有行和所有列?

您可能需要考虑的其他一些事项:

userAnswersReverse[rows-1-q][cols-1-q] = userAnswersInt[q][w];

您可能希望在赋值两侧的列索引中使用“w”而不是“q”。

此外,考虑到您试图反转一行内的值,而不是交换行的顺序,该行的索引是否应该在两边都相同?

另一个有问题的行:

for(int w = cols-1; w >=0; w++){

看起来你是在向上而不是向下数。

于 2013-03-15T23:38:25.303 回答