我试图计算文件中具有匹配答案的每一行作为考试解决方案,并使用考试解决方案计算每行正确答案的数量。我试图计算具有正确答案的人数作为存储在类中的测试答案,一旦读取文件并将文件数组存储到类中 - 在将文件数组放入类构造函数以计算答案之后,但我可以'不,因为我得到了班级不计算的错误答案。你如何在Java中解决这个问题,我可以计算文件数组并将其存储到类中?
考试文件3.txt:
A A A A A A B A A A B C A A A A C B D A
B D A A C A B A C D B C D A D C C B D A
B D A A C A B A C D B C D A D C C B D A
考试解决方案:BDAACABACDBCDADCCBDA
这是课程:
import java.io.*;
import java.util.*;
/*
* http://www.java-examples.com/read-char-file-using-datainputstream
*/
public class Exam {
private static boolean pass; //passed if student passes exam
private static int allRightQuestions=0; //counts number of people having full marks
private static int lessThan5score=0; //counts number of people having < 5 scores
private static int lessThan10Score=0; //counts number of people having < 10 scores
private static int totalCorrect; //number of questions being answered correctly
private static int totalIncorrect; //number of questions being answered wrongly
private static int questionsMissed[][]; //array for storing the missed question numbers
private static int passNo = 0; //number of people passing by having at least 15/20
private static int failNo = 0; //number of people failing by having less than 15/20
private static int row = 0;//for counting rows of 20 answers
private static int column = 0; //for counting letter exam answers
private static String showanswers = new String("B D A A C A B A C D B C D A D C C B D A");//the exam solution
String[] realanswers = new String[20];
private static String showresults[];
public Exam(String[] showresult, int rows)
{
// TODO Auto-generated constructor stub
showresult = new String[rows];
showresults = showresult;
row = rows;
}
public void sequentialsearch() //check the student having full marks
{
for ( row = 0; row<= showresults.length; row++)
if (showresults[row].equals(showanswers ))
{
pass = true;
passNo +=1;
allRightQuestions +=1;
}
else
{
pass = false;
}
getallright();//counts number of people having full marks
passNo();//count number of people passing
}
public int getallright()
{
return allRightQuestions;
}
public int passNo() //shows number of people who pass
{
return passNo;
}
public void checkwronganswers(String[] showresult, int row) //working out the number of wrong and right answers in test
{
boolean found = false;
int failure =0;
for ( row = 0; row <= showresult.length; row++)
for ( column = 0; column <= 20; column+=2)
{
if (showanswers.charAt(column) == showresults[row].charAt(column))
{
found = true;
totalCorrect +=1;
}
if (totalCorrect > 14 && totalCorrect <=20)
{
passNo += 1;
passNo();
}
else
{
failNo +=1;
fail();
}
if ( totalCorrect < 10)
{
lessThan10Score +=1;
lessthan10right();
failure += lessThan10Score;
}
if ( totalCorrect < 5)
{
lessThan5score +=1;
lessthan5right();
failure += lessThan5score;
}
}
failNo = failure;
fail();
}
public int fail()//number of people who failed
{
return failNo;
}
public int lessthan5right()//number of people having < 5 right answers
{
return lessThan5score;
}
public int lessthan10right()//number of people having < 5 right answers
{
return lessThan10Score;
}
public String show()
{
return showresults[0];
}
public boolean passed()
{
return pass;
}
public int totalIncorrect()
{
return totalIncorrect;
}
public int totalCorrect()
{
return totalCorrect;
}
public int questionsMissed()
{
return questionsMissed[0][0];
}
}
这是程序:
import java.io.*;
import java.util.Scanner;
public class ExamDemo {
private static Scanner kb;
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
int row = 0;
String[] showresults = new String[30]; //student answers
kb = new Scanner(System.in);
System.out.println(" Enter text:" );
String fileName = kb.nextLine();
File file = new File(fileName);
Scanner inputfile = new Scanner(file);
while( inputfile.hasNext() && row< showresults.length)
{
showresults[row] = inputfile.nextLine();
System.out.println( "row" + row + ":" + showresults[row]);
row++;
}
System.out.println( row );
showresults = new String[row];
System.out.println( "accepted" );
Exam exam = new Exam(showresults, row);
System.out.println( "reached");
System.out.println("students having all questions correct=" + exam.getallright());
System.out.println("students having passed the questions = " + exam.passNo() );
System.out.println("students having failed the questions = " + exam.fail() );
System.out.println("students having less than
5 the questions right= " + exam.lessthan5right() );
System.out.println("students having less than 10
the questions right= " + exam.lessthan10right() );
exam.show();
inputfile.close();
}
}