我正在尝试做的只是反转每行中的数字,因此 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