我的代码已经完成并且大部分时间都在工作,这就是我所拥有的
import java.util.*;
import java.io.*;
import java.text.*;
public class Proj5 {
public static void main(String[] args)throws IOException{
Scanner s = new Scanner(System.in);
PrintWriter outFile = new PrintWriter(new File("results.txt"));
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 [] numCorrect = new int[quizKey.length];
DecimalFormat df = new DecimalFormat("#0.0");
int max;
int min;
int lines=0;
readInText(s);
s = readInText(s);
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, numCorrect);
char[] grades = new char[lines];
grades = grade(numCorrect, lines);
max=max(numCorrect);
min=min(numCorrect);
displayOutput(outFile, wid, lines, numCorrect, grades, percentCorrect);
averageScore(outFile, max, min, lines, percentCorrect);
}//end main
public static Scanner readInText(Scanner s)throws IOException{
/*String input;
System.out.print("Enter name of the file: ");
input=s.nextLine();
System.out.println(input);
while(input!="QuizScores.txt"){
System.out.println("invalid file entered, please enter the correct anem of the file: ");
input=s.nextLine();
}
*/
Scanner inFile = new Scanner(new File("QuizScores.txt"));
return inFile;
}// end readInText
public static String[] userAnswers(String userAnswers[]){
return userAnswers;
}
/**
* calculates the grades of the tests
*
* @param lines - number of
* @param quizKey - answers to the test
* @param userAnswers - string version of user answers
* @param numCorrect - number of answers correct
* @param userAnswersInt - int version of user answers
* @return numCorrect - number of answers correct
*/
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++){
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];
}
}
}
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
/**
* calculates the max
*
* @param numCorrect - number of correct answers
* @return max - the highest number of correct answers
*/
public static int max(int numCorrect[]){
int max = numCorrect[0];
for(int r=0; r<numCorrect.length; r++){
if(numCorrect[r]>max){
max=numCorrect[r];
}
}
return max;
}
/**
* calculates the min
*
* @param numCorrect - number of correct answers
* @return min - the lowest number of correct answers
*/
public static int min(int numCorrect[]){
int min = numCorrect[0];
for(int r=0; r<numCorrect.length; r++){
if(numCorrect[r]<min){
min=numCorrect[r];
}
}
return min;
}
/**
* grade of the test
*
* @param numCorrect - number of answers correct
* @param lines - number of lines in file
* @return grade - the letter grade is being returned)
*/
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
/**
* percent of the answers correct
*
* @param lines - number of lines in file
* @param numCorrect - number of answers correct
* @param quizKey - correct answers
* @return centCorrect - percent of correct answers (1)
*/
public static double[] percentCorrect(int lines, int numCorrect[], int quizKey[]){
double[] centCorrect = new double[lines];
for (int j=0; j<lines; j++){
centCorrect[j] = (numCorrect[j]/quizKey.length)*100;
}
return centCorrect;
}
/**
* average of all the test scores & displays min and max
*
* @param min - lowest score
* @param max - highest score
* @param lines - number of lines in file
* @param percentCorrect - percent of correct answers
* @return none
*/
public static void averageScore(PrintWriter outFile, 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
add=add/lines;
System.out.println("Average: " + add + "%");
outFile.println("Average: " + add + "%");
System.out.println("High Score: " + max);
outFile.println("High Score: " + max);
System.out.println("Low Score: " + min);
outFile.println("Low Score: " + min);
}// end averageScore
/**
* displays the student id number correct percent correct and grade
*
* @param wid - wildcat ID
* @param lines -number of lines in file
* @param numCorrect - number of answers correct
* @param grades - grade of test
* @param percentCorrect - percent of answers correct
* @return none
*/
public static void displayOutput( PrintWriter outFile, String wid[], int lines, int numCorrect[], char grades[], double percentCorrect[]){
System.out.println("Student ID # Correct %Correct Grade");
outFile.println("Student ID # Correct %Correct Grade");
for(int i=0; i<lines; i++){
System.out.println(" " + wid[i] + " " + numCorrect[i] + " " +
(percentCorrect[i]) + "%" + " " + grades[i]);
outFile.println(" " + wid[i] + " " + numCorrect[i] + " " +
(percentCorrect[i]) + "%" + " " + grades[i]);
}
}// end display output
}//end class
这是我不包括用户输入要求时得到的输出:
Student ID # Correct %Correct Grade
4563123 15 100.0% A
2312311 9 0.0% D
2312345 13 0.0% B
5527687 9 0.0% D
7867567 6 0.0% F
Average: 20.0%
High Score: 0
Low Score: 15
所以我将从我的问题开始,总的来说它工作得很好,只是由于某种原因没有将值分配给 percentCorrect 数组(我认为不是 100%)另外还有高分(最大值)和低分(最小值)工作不正常,我也不知道为什么会这样,所以对这些事情的帮助会很棒,我完全被难住了。
我的另一个问题是,我需要让用户输入文件名,如果他们输入了错误的文件名,让他们知道他们做了并再次询问,直到他们做对了,我实现了这个:
String input;
System.out.print("Enter name of the file: ");
input=s.nextLine();
System.out.println(input);
while(input!="QuizScores.txt"){
System.out.println("invalid file entered, please enter the correct anem of the file: ");
input=s.nextLine();
}
但它不起作用,因为即使我输入正确的字符串(大写字母和所有内容),它仍然会打印“无效的文件,请...”行我也不知道我在这里做错了什么,所以请帮忙这太棒了。
提前致谢
编辑:
好的,我认为问题可能出在这些代码块中(同样不是 100%)
percentCorrect = percentCorrect(lines, numCorrect, numCorrect);
和/或
public static double[] percentCorrect(int lines, int numCorrect[], int quizKey[]){
double[] centCorrect = new double[lines];
for (int j=0; j<lines; j++){
centCorrect[j] = (numCorrect[j]/quizKey.length)*100;
}
return centCorrect;
}
和/或
public static int max(int numCorrect[]){
int max = numCorrect[0];
for(int r=0; 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=0; r<numCorrect.length; r++){
if(numCorrect[r]<min){
min=numCorrect[r];
}
}
return min;
}
我从中提取的文件是这样的:
4563123,112211324135412
2312311,222121324135211
2312345,112211324135421
5527687,212111313124412
7867567,111111111111111