私がやろうとしていることは、各行の数字だけを逆にするので、row [0]の列が逆になるなどです。両方をオンラインで見る方法はわかりましたが、必要なことを行う方法がわかりません。行う。さらに、nullエラーが発生し、なぜそれが発生しているのか理解できません。コード全体は次のとおりです。
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