首页 > 编程语言 >前三次答题判题程序总结

前三次答题判题程序总结

时间:2024-04-21 17:44:31浏览次数:32  
标签:String 答题 int ++ num 判题 三次 new public

前言:
本次作业主要考察类的设计,从第一次作业只需三到五个类就能解决问题到第三次需要六七个类以上才能解决问题,这要求我们在写程序之前必须设计好各个类之间的关系和职责,这需要我们有良好的设计习惯并且遵守面向对象程序设计的原则。这几次作业难度很大,第一次作业200行可以搞定,第二次作业起码要400行,第三次作业就来到了500行代码。
每次作业纯编码时间可能就需要20小时以上。难度可以说非常大。

设计与分析:
因为本体需要分析输入的各种复杂信息,所以学习正则表达式的使用是必要的。
第一次作业需要读取
1、 题目数量,格式为整数数值,若超过1位最高位不能为0;
2、 题目内容,格式为:"#N:"+题号+" "+"#Q:"+题目内容+" "#A:"+标准答案;
格式约束:题目的输入顺序与题号不相关,不一定按题号顺序从小到大输入。
3、答题信息,答题信息按行输入,每一行为一组答案,每组答案包含第2部分所有题目的解题答案,答案的顺序号与题目题号相对应。
格式:"#A:"+答案内容
格式约束:答案数量与第2部分题目的数量相同,答案之间以英文空格分隔。
样例:#A:2 #A:78
所以以下三个类是需要的。
题目类(Question):用于保存一道题目的信息以及处理的方法。
private int num;// 题号
private String content, // 题目内容
standardAnswer;// 标准答案
boolean matchingStandardAnswers(String answer):判断是否符合标准答案
试卷类(TestPaper):保存和处理一张试卷的信息以及处理的方法
int questionNum;// 题目数量
Question[] questions;//题目清单
void inputQuestion(int num, Question question):添加一道题目
void printQuestions():输出题目的内容~标准答案
boolean markQuestion(int num, String answer):判断第 num 题的正确性。
答卷类(AnswerPaper):保存和处理一张答卷的信息以及处理的方法
TestPaper paper;//试卷信息
String[] answers;//每一题的答案信息
boolean[] marks;//每一题的判题结果(对/错)
void printQ_A(int num):输出第 num 题的题目和答案(卷面答案,非标准答案)
boolean getJudge(int num):获得第 num 题的判题结果
由于输入信息需要用正则表达式来读取,所以最好还要设计一个用来分析信息的类。
匹配输入类(InputMatching)
questionNumMatching:String =””//正则表达式用于匹配题号
questionMatching:String=””//匹配题目内容
standerAnswerMatching:String=“”//匹配标准答案
questionInput:String = questionNumMatching+“”+ questionMatching+“”standerAnswerMatching
answerMatching: String = “”//匹配答案
具体实现是这样的

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//首先读取题目数量
int number = input.nextInt();
//这一步是为了将换行符读取,以避免影响后续字符串的输入
String skipEnter = input.nextLine();
//创建问题类数组存取各个问题
Question[] q = new Question[number];
//创建字符串数组,将接下来所有要输入的字符串按行读入
String[] k = new String[number];
//设计正则表达式来分析各个字符串,建议大家这部分可以单独创建一个类,方便后续进行拓展,当时写这里的时候完全没考虑拓展的问题。
、、匹配题号
String regexN = "#N:(.)#Q";
、、匹配题目内容
String regexQ = "#Q:(.
)#";
、、匹配标准答案
String regexA = "#A:(.*)";
Pattern patternN = Pattern.compile(regexN);
Pattern patternQ = Pattern.compile(regexQ);
Pattern patternA = Pattern.compile(regexA);
Matcher matcherA;
//将题目信息读入
for (int i = 0; i < number; i++) {
q[i] = new Question();
k[i] = input.nextLine();
Matcher matcherN = patternN.matcher(k[i]);
Matcher matcherQ = patternQ.matcher(k[i]);
matcherA = patternA.matcher(k[i]);
if(matcherN.find()) {
String num = matcherN.group(0);
//System.out.println(num);
num = num.substring(3,num.length() - 2);
int temp = Integer.parseInt(num.trim());
q[i].setID(temp);
}
if(matcherQ.find()){
String content = matcherQ.group(0);
content = content.substring(3,content.length() - 2);
q[i].setContent(content.trim());
//System.out.println(q[i].getContent());
}
if(matcherA.find()){
String standardAnswer = matcherA.group(0);
standardAnswer =standardAnswer.substring(3);
q[i].setStandardAnswer(standardAnswer.trim());
//System.out.println(q[i].getStandardAnswer());
}
}
//将答案读入
String str = input.nextLine();
String regex1 = "#A:(\w+)";
Pattern pattern = Pattern.compile(regex1);
matcherA = pattern.matcher(str);
String[] answerList = new String[100];
for(int i = 0; i < number; i++){
if(matcherA.find()){
answerList[i] = matcherA.group(0);
//System.out.println(answerList[i]);
answerList[i] = answerList[i].substring(3);
//System.out.println(answerList[i]);
}
}
str = input.nextLine();
// if(str.equals("end")){
// input.close();
// }
、、创建试卷类,并将问题数组和问题数量传给试卷类
ExaminationPaper exam = new ExaminationPaper(q,number);
boolean[] isTrueList = new boolean[100];
、、将问题按题号顺序排好
if(number >= 2){
for(int i = 0; i < number - 1; i++){
for(int j = 0; j < number - 1; j++){
if(q[j].getID() > q[j + 1].getID()){
Question temp = q[j + 1];
q[j + 1] = q[j];
q[j] = temp;
// String cnt = answerList[j];
// answerList[j] = answerList[j + 1];
// answerList[j + 1] = cnt;
}
}
}
}
、、将答案按格式输出
for(int i = 0; i < number; i++){
// System.out.println(q[i].getStandardAnswer());
// System.out.println(answerList[q[i].getID() - 1]);
isTrueList[q[i].getID() - 1] = q[i].getStandardAnswer().equals(answerList[q[i].getID() - 1]);
}
AnswerSheet answerSheet = new AnswerSheet( exam, answerList,isTrueList);
for(int i = 0; i < number; i++){
answerSheet.print(q[i].getID());
}
for(int i = 0; i < number; i++){
if(i + 1 == number){
System.out.print(isTrueList[i]);
}else{
System.out.print(isTrueList[i] + " ");
}
}
}
}

class Question{
private int ID;
private String content;
private String standardAnswer;

public int getID() {
    return ID;
}

public void setID(int ID) {
    this.ID = ID;
}

public String getContent() {
    return content;
}

public void setContent(String content) {
    this.content = content;
}

public String getStandardAnswer() {
    return standardAnswer;
}

public void setStandardAnswer(String standardAnswer) {
    this.standardAnswer = standardAnswer;
}

public Question() {
}

public Question(int ID, String content, String standardAnswer) {
    this.ID = ID;
    this.content = content;
    this.standardAnswer = standardAnswer;
}
public boolean isTrue(String answer){
    return answer.equals(standardAnswer);
}

}
class ExaminationPaper {
public ExaminationPaper() {
}

public ExaminationPaper(Question[] questions, int number) {
    this.questions = questions;
    this.number = number;
}

private Question[] questions = new Question[100];

private int number;

public int getNumber() {
    return number;
}

public void createArray(){
    for(int i = 0; i < 100; i++) {
        questions[i] = new Question();
    }
}
public Question[] getQuestions(){
    return questions;
}
public void saveQuestions(int number, String question) {

// for(int i = 0; i < 100; i++) {
// questions[i] = new Question();
// }
questions[number].setContent(question);
questions[number].setID(number);
}

public boolean isTrue(int num, String answer) {
    return questions[num].isTrue(answer);
}

}
class AnswerSheet{
private ExaminationPaper paper = new ExaminationPaper();
private String[] answer = new String[100];
private boolean[] isTrue = new boolean[100];

public AnswerSheet(ExaminationPaper paper, String[] answer, boolean[] isTrue) {
    this.paper = paper;
    this.answer = answer;
    this.isTrue = isTrue;
}

public String[] getAnswer() {
    return answer;
}

public void setAnswer(String[] answer) {
    this.answer = answer;
}

public boolean[] getIsTrue() {
    return isTrue;
}

public void setIsTrue(boolean[] isTrue) {
    this.isTrue = isTrue;
}

public AnswerSheet() {
}
public boolean judgment(int num){
    return paper.isTrue(num,answer[num]);
}
public void print(int num){
    Question[] question = paper.getQuestions();
    System.out.print(question[num - 1].getContent().trim());
    System.out.print("~");
    System.out.println(answer[num - 1]);
}
public void saveAnswer(int num, String str){
    answer[num] = str;
}

}

第二次作业相比于第一次作业,有一些输入的东西变多了,有部分输入的格式也有所改变,并且输入变为乱序输入,这里要注意一些很恶心细节问题,例如答卷答案与问题的匹配顺序,可以看到答案的顺序与试卷中的题目顺序是一致的。我帮大家理解理解这句话,因为这个题目非常长,通读完整个题目后不免会将一些细节忘记,而就是这些微小的细节,会让你在后续修改bug的过程非常痛苦。回到正题,例如试卷是#T:1 3-5 4-8 5-2,答卷是#S:1 #A:5 #A:22,这意味着#A:5是第3题的答案,#A:22第四题的答案,第5题没有答案。大家一定要创建一个记事本,把一些关键的要求记下来,不然纯靠脑子记真的会搞晕。第二次还涉及到了多张试卷多张答卷的问题,具体的看接下来的源码

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//int number = input.nextInt();
final int number = 100;

    //String skipEnter = input.nextLine();
    String[] answerList = new String[100];
    Question[] q = new Question[number];
    AnswerSheet[] a = new AnswerSheet[10];
    ExaminationPaper[] e = new ExaminationPaper[10];
    String[] k = new String[number];
    int[] numList = new int[100];
    int[] scoreList = new int[100];
    int j = 0;
    String regexN = "#N:(.*)#Q";
    String regexQ = "#Q:(.*)#";
    String regexA = "#A:(.*)";
    String regexT = "#T:(\\w+)";
    String regexS = "#S:(\\w+)";
    String regex1 = "#A:(\\w+)";
    String regexNum =" [\\w\\-]+";

    Pattern patternN = Pattern.compile(regexN);
    Pattern patternQ = Pattern.compile(regexQ);
    Pattern patternA = Pattern.compile(regexA);
    Pattern patternT = Pattern.compile(regexT);
    Pattern patternS = Pattern.compile(regexS);
    Pattern pattern1 = Pattern.compile(regex1);
    Pattern patternNum = Pattern.compile(regexNum);
    //将题目信息读入
    for (int i = 0; i < number; i++) {
        k[i] = input.nextLine();
        if(k[i].equals("end")){
            break;
        }
        Matcher matcherN = patternN.matcher(k[i]);
        Matcher matcherQ = patternQ.matcher(k[i]);
        Matcher matcherA = patternA.matcher(k[i]);
        Matcher matcherS = patternS.matcher(k[i]);
        Matcher matcherT = patternT.matcher(k[i]);
        Matcher matcher1 = pattern1.matcher(k[i]);
        Matcher matcherNum = patternNum.matcher(k[i]);

        if(matcherN.find()) {
            String num = matcherN.group(0);
            //System.out.println(num);
            num = num.substring(3,num.length() - 2);
            int temp = Integer.parseInt(num.trim());
            q[temp] = new Question();
            q[temp].setID(temp);
            if(matcherQ.find()){
                String content = matcherQ.group(0);
                content = content.substring(3,content.length() - 2);
                q[temp].setContent(content);
                //System.out.println(q[i].getContent());
            }
            if(matcherA.find()){
                String standardAnswer = matcherA.group(0);
                standardAnswer =standardAnswer.substring(3);
                q[temp].setStandardAnswer(standardAnswer);
                //System.out.println(q[i].getStandardAnswer());
            }
        }

        if(matcherS.find()) {
            String id = matcherS.group(1).trim();
            int ID = Integer.parseInt(id);
            if (a[ID] != null) {
                ID++;
                a[ID] = new AnswerSheet();
                a[ID].setID(ID - 1);
            }else {
                a[ID] = new AnswerSheet();
                a[ID].setID(ID);
            }
                int[] temp = new int[100];
            if(e[a[ID].getID()] != null) {
                temp = e[a[ID].getID()].getNumList();
            }else{
                break;
            }
                // System.out.println(e[ID].getNumLength());
                for (i = 0; i < 10; i++) {
                    answerList[i] = null;
                }
                int w = 0;
                while (w <= e[a[ID].getID()].getNumLength()) {
                    if (matcher1.find()) {
                        int p = temp[w];

                        answerList[p] = matcher1.group(0);
                        //System.out.println(answerList[i]);
                        //answerList[ID] = new String();
                        answerList[p] = answerList[p].substring(3);
                        //System.out.println(answerList[i]);
                    }
                    w++;
                }
                a[ID].setAnswer(answerList);

// if(a[1] != null) {
// for (i = 0; i < 100; i++) {
// System.out.println(a[1].getAnswer()[i]);
// }
// }
}
if(matcherT.find()){
String id = matcherT.group(0);
id = id.substring(3,id.length()).trim();
int ID = Integer.parseInt(id);
j = 0;
for(i = 0; i < j; i++){
numList[i] = 0;
scoreList[i] = 0;
}
while(matcherNum.find()){
String num = matcherNum.group(0);
int temp = num.indexOf("-");
String forward = num.substring(1, temp).trim();
String behind = num.substring(temp + 1).trim();
int f = Integer.parseInt(forward);
int b = Integer.parseInt(behind);
numList[j++] = f;
scoreList[f] = b;
}
e[ID] = new ExaminationPaper();
e[ID].setNumLength(j);
e[ID].setID(ID);
e[ID].setNumList(numList);
e[ID].setScoreList(scoreList);
}
}
//System.out.println(e[1].getID());
// 如何按入顺序匹配答案?
boolean[] isTrueList = new boolean[100];
int cnt = 1;
//判题
for(int i = 1; e[i] != null; i++){
e[i].setQuestions(q);
for(int p = 1; a[p] != null; p++){
a[p].setPaper(e[a[cnt].getID()]);
int cnt1 = 0;
int num = 0;
do{
if(e[a[p].getID()] != null) {
num = e[a[p].getID()].getNumList()[cnt1];
}else{
break;
}
isTrueList[num] = q[num].getStandardAnswer().equals(a[p].getAnswer()[num]);
a[p].setIsTrue(isTrueList);
cnt1++;
}while(e[a[p].getID()].getNumList()[cnt1] != 0);
}
}
cnt = 1;
//输出答案
while(e[cnt] != null){
if(e[cnt].totalPoint() == 100){
cnt++;
continue;
}else{
System.out.printf("alert: full score of test paper%d is not 100 points\n",e[cnt].getID());
}
cnt++;
}
cnt = 1;
int p = 0;
for(int i = 1; e[i] != null; i++){
for(p = 1; a[p] != null; p++){
if(a[p].getID() == e[i].getID()) {
for (int t = 0; t < e[a[p].getID()].getNumLength(); t++) {
int z = e[a[p].getID()].getNumList()[t];
a[p].print(z);
}
a[p].gotPointPrint(e[i].getNumList(), e[i].getNumLength());
}
}
if(a[p] == null){
if(a[p-1] != null){
continue;
}
System.out.print("The test paper number does not exist");
break;
}
}

}

}

class Question{
private int ID;
private String content;
private String standardAnswer;

public int getID() {
    return ID;
}

public void setID(int ID) {
    this.ID = ID;
}

public String getContent() {
    return content;
}

public void setContent(String content) {
    this.content = content;
}

public String getStandardAnswer() {
    return standardAnswer;
}

public void setStandardAnswer(String standardAnswer) {
    this.standardAnswer = standardAnswer;
}

public Question() {
}

public Question(int ID, String content, String standardAnswer) {
    this.ID = ID;
    this.content = content;
    this.standardAnswer = standardAnswer;
}
public boolean isTrue(String answer){
    return answer.equals(standardAnswer);
}

}
class ExaminationPaper {
final int n = 10;
private int numLength;
private int ID;
private Question[] questions = new Question[100];
private int[] numList = new int[100];
private int[] scoreList = new int[100];

private int number;
public ExaminationPaper() {
}

public ExaminationPaper(int numLength, int ID, int[] numList) {
    this.numLength = numLength;
    this.ID = ID;
    this.numList = numList;
}

public ExaminationPaper(Question[] questions, int number, int ID) {
    this.questions = questions;
    this.number = number;
    this.ID = ID;
}

public int getNumLength() {
    return numLength;
}

public void setNumLength(int numLength) {
    this.numLength = numLength;
}

public int[] getNumList() {
    return numList;
}

public int getN() {
    return n;
}

public int[] getScoreList() {
    return scoreList;
}

public void setScoreList(int[] scoreList) {
    for(int i = 0; i < scoreList.length; i++){
        this.scoreList[i] = scoreList[i];
    }
}

public void setNumList(int[] numList) {
    for(int i = 0; i < numLength; i++){
        this.numList[i] = numList[i];
    }
}

public int getID() {
    return ID;
}

public void setID(int ID) {
    this.ID = ID;
}

public void setQuestions(Question[] questions) {
    for(int i = 0; i < n; i++){
        this.questions[i] = questions[i];
    }

}

public void setNumber(int number) {
    this.number = number;
}

public int getNumber() {
    return number;
}

public void createArray(){
    for(int i = 0; i < 100; i++) {
        questions[i] = new Question();
    }
}
public Question[] getQuestions(){
    return questions;
}
public void saveQuestions(int number, String question) {

// for(int i = 0; i < 100; i++) {
// questions[i] = new Question();
// }
questions[number].setContent(question);
questions[number].setID(number);
}
public int totalPoint(){
int sum = 0;
int i = 0;
do{
sum += scoreList[numList[i]];
i++;
}while(numList[i] != 0);
return sum;
}

public boolean isTrue(int num, String answer) {
    return questions[num].isTrue(answer);
}

}
class AnswerSheet{
final int n = 10;
private ExaminationPaper paper = new ExaminationPaper();
private String[] answer = new String[100];
private boolean[] isTrue = new boolean[100];
private int ID;

public AnswerSheet(int ID, ExaminationPaper paper, String[] answer, boolean[] isTrue) {
    this.paper = paper;
    this.answer = answer;
    this.isTrue = isTrue;
    this.ID = ID;
}

public ExaminationPaper getPaper() {
    return paper;
}

public void setPaper(ExaminationPaper paper) {
    this.paper = paper;
}

public int getID() {
    return ID;
}

public void setID(int ID) {
    this.ID = ID;
}

public String[] getAnswer() {
    return answer;
}

public void setAnswer(String[] answer) {
    for(int i = 0; i < n; i++){
        this.answer[i] = answer[i];
    }

}

public boolean[] getIsTrue() {
    return isTrue;
}

public void setIsTrue(boolean[] isTrue) {
    for (int i = 0; i < n; i++) {
        this.isTrue[i] = isTrue[i];
    }
}

public AnswerSheet() {
}
public boolean judgment(int num){
    return paper.isTrue(num,answer[num]);
}
public void print(int num){
    //Question[] question = paper.getQuestions();
    if(answer[num] != null){
    System.out.print(paper.getQuestions()[num].getContent().trim());
    System.out.print("~");
    System.out.print(answer[num]);
    System.out.print("~");
    System.out.println(isTrue[num]);
    }else{
        System.out.println("answer is null");
    }
}
public void gotPointPrint(int[] numlist, int lenth){
    int got = 0;
    int j = 0;
    for(;j < lenth; j++) {
        int i = numlist[j];
        if (isTrue[i]) {
            got += paper.getScoreList()[i];
            System.out.print(paper.getScoreList()[i]);
            if(j + 1 != lenth) {
                System.out.print(" ");
            }
        }else{
            System.out.print("0");
            if(j + 1 != lenth) {
                System.out.print(" ");
            }
        }
    }
    System.out.print("~");
    System.out.println(got);
}
public void saveAnswer(int num, String str){
    answer[num] = str;
}

}

第三次相比于第二次多增加了格式的检测,这就要求对正则表达式的使用要更加熟练。由于前两次没有自己设计类图,而且仅仅凭着题目的建议和自己的感觉硬写,导致写到后期思路很乱,各个类之间的关系不明了,也没做到单一职责,这导致后期代码量上来以后,每次开始写自己代码前还要浪费很多时间去回想这些代码的作用。痛定思痛,我终于明白设计的重要性,于是在第三次作业开始后,我并没有着急的进行编码,而是先使用powerdesigner进行了类的设计,如图

当时设计的时候还很青涩,如果你有能力的话可以设计更加完善的类图,如下

设计类图的好处是非常多的,其中我最喜欢的一点是我的心里对这个500多行的程序有了一个很清晰的结构。接下来看源码:

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
public static void main(String[] args) {

    //初始化变量
    Scanner input = new Scanner(System.in);
    final int number = 100;
    String[] answerList = new String[100];
    int[] answerNumberList = new int[100];
    Question[] Q = new Question[number];
    AnswerSheet[] A = new AnswerSheet[10];
    ExaminationPaper[] E = new ExaminationPaper[10];
    Student[] S = new Student[10];
    Judgment[] J = new Judgment[10];
    String[] k = new String[number];
    int[] numList = new int[100];
    int[] scoreList = new int[100];
    int[] deleteList = new int[100];
    //用于迭代T
    int j = 0;
    //用于迭代A
    int cntA = 0;
    //用于迭代deleteList
    int cntD = 0;

    String regexN = "#N:(.*)#Q";
    String regexN1 = "^#N:(\\d+) (#Q:[^#]*) (#A:(.*)){1}$";
    String regexQ = "#Q:(.*)#";
    String regexA = "#A:(.*)";
    String regexA1 = "#A:(.*)";
    String regexT = "#T:(\\w+)";
    //检测是否符合问题的格式
    String regexT1 = "#T:\\d+(\\s\\d-\\d)*(\\s\\d-\\d)$";
    String regexS = "#S:(\\w+)";
    String regexS1 = "#S:\\d+ \\d+(.*)";
    String regex1 = "#A:(\\d-[\\w.]*)";
    String regexNum = " [\\w\\-]+";
    String regexX = "#X(.*)";
    String regexX1 = "#X:(\\d{8} \\w*-)*(\\d{8} \\w{3})$";
    String regexD = "#D(.*)";
    String regexD1 = "#D:(N-\\d+)$";
    String regexID = "\\w+";
    String regexS2 = "( \\d+)";
    String regexD2 = "(\\d+)";

    Pattern patternN = Pattern.compile(regexN);
    Pattern patternQ = Pattern.compile(regexQ);
    Pattern patternA = Pattern.compile(regexA);
    Pattern patternT = Pattern.compile(regexT);
    Pattern patternS = Pattern.compile(regexS);
    Pattern pattern1 = Pattern.compile(regex1);
    Pattern patternNum = Pattern.compile(regexNum);
    Pattern patternX = Pattern.compile(regexX);
    Pattern patternD = Pattern.compile(regexD);
    Pattern patternID = Pattern.compile(regexID);
    Pattern patternS2 = Pattern.compile(regexS2);
    Pattern patternD2 = Pattern.compile(regexD2);
    Pattern patternD1 = Pattern.compile(regexD1);
    Pattern patternN1 = Pattern.compile(regexN1);
    Pattern patternS1 = Pattern.compile(regexS1);
    Pattern patternX1 = Pattern.compile(regexX1);
    Pattern patternT1 = Pattern.compile(regexT1);

    //将题目信息读入
    for (int i = 0; i < number; i++) {
        k[i] = input.nextLine();
        if (k[i].equals("end")) {
            break;
        }
        Matcher matcherN = patternN.matcher(k[i]);
        Matcher matcherQ = patternQ.matcher(k[i]);
        Matcher matcherA = patternA.matcher(k[i]);
        Matcher matcherS = patternS.matcher(k[i]);
        Matcher matcherT = patternT.matcher(k[i]);
        Matcher matcher1 = pattern1.matcher(k[i]);
        Matcher matcherNum = patternNum.matcher(k[i]);
        Matcher matcherX = patternX.matcher(k[i]);
        Matcher matcherD = patternD.matcher(k[i]);
        Matcher matcherS2 = patternS2.matcher(k[i]);
        Matcher matcherD2 = patternD2.matcher(k[i]);
        Matcher matcherN1 = patternN1.matcher(k[i]);
        Matcher matcherT1 = patternT1.matcher(k[i]);
        Matcher matcherS1 = patternS1.matcher(k[i]);
        Matcher matcherD1 = patternD1.matcher(k[i]);
        Matcher matcherX1 = patternX1.matcher(k[i]);


        if (matcherN1.find()) {
            if (matcherN.find()) {
                String num = matcherN.group(0);
                //System.out.println(num);
                num = num.substring(3, num.length() - 2);
                int temp = Integer.parseInt(num.trim());
                Q[temp] = new Question();
                Q[temp].setQuestionNumber(temp);
                if (matcherQ.find()) {
                    String content = matcherQ.group(0);
                    content = content.substring(3, content.length() - 2);
                    Q[temp].setContent(content);
                    //System.out.println(Q[i].getContent());
                }
                if (matcherA.find()) {
                    String standardAnswer = matcherA.group(0);
                    standardAnswer = standardAnswer.substring(3);
                    Q[temp].setStandardAnswer(standardAnswer);
                    //System.out.println(Q[i].getStandardAnswer());
                }
            }
        }
        else if (matcherS1.find()) {
            if (matcherS.find()) {
                String id = matcherS.group(1).trim();
                if (matcherS2.find()) {
                    String studentNumber = matcherS2.group(0).trim();
                    int ID = Integer.parseInt(id);
                    A[cntA] = new AnswerSheet();
                    A[cntA].setAnswerSheetNumber(ID);
                    A[cntA].setStudentNumber(studentNumber);
                    int p = 0;
                    for (int la = 0; la < 10; la++) {
                        answerList[la] = null;
                        answerNumberList[la] = 0;
                    }
                    while (matcher1.find()) {
                        String temp = matcher1.group(0);
                        String str = temp.substring(3, 4);
                        String temp1 = temp.substring(5);
                        answerNumberList[p] = Integer.parseInt(str);
                        answerList[p] = temp1;
                        p++;
                    }
                    A[cntA].setAnswerList(answerList);
                    A[cntA].setAnswerNumberList(answerNumberList);
                    cntA++;

                }
            }
        }
        else if (matcherT1.find()) {
            matcherT1.reset();
            if (matcherT.find()) {
                String id = matcherT.group(0);
                id = id.substring(3, id.length()).trim();
                int ID = Integer.parseInt(id);

                for (i = 0; i < j; i++) {
                    numList[i] = 0;
                    scoreList[i] = 0;
                }
                int z = 0;
                if (matcherNum.find()) {
                    matcherNum.reset();
                    while (matcherNum.find()) {
                        String num = matcherNum.group(0);
                        int temp = num.indexOf("-");
                        String forward = num.substring(1, temp).trim();
                        String behind = num.substring(temp + 1).trim();
                        int f = Integer.parseInt(forward);
                        int b = Integer.parseInt(behind);
                        numList[z] = f;
                        scoreList[z] = b;
                        z++;
                    }
                    E[j] = new ExaminationPaper();
                    E[j].setTestNumber(ID);
                    E[j].setQuestionNumberList(numList);
                    E[j].setQuestionScoreList(scoreList);
                    E[j].setQuestions(Q);
                    j++;
                }
            }
        }
        else if (matcherX1.find()) {
            if(matcherX.find()) {
                String str = k[i].substring(3);
                Matcher matcherID = patternID.matcher(str);
                int cnt = 0;
                String name = "";
                String studentNumber = "";
                boolean temp = false;
                while (matcherID.find()) {
                    if (temp) {
                        name = matcherID.group(0);
                        temp = false;
                        S[cnt].setName(name);
                        cnt++;
                    } else {
                        S[cnt] = new Student();
                        studentNumber = matcherID.group(0);
                        temp = true;
                        S[cnt].setStudentNumber(studentNumber);
                    }


                }
            }
        }
        else if (matcherD1.find()) {
            if(matcherD.find()) {
                while (matcherD2.find()) {
                    deleteList[cntD++] = Integer.parseInt(matcherD2.group(0));
                }
            }
        }
        else{
            //若格式错误
            System.out.println("wrong format:" + k[i]);
        }
    }

    //进行删除操作
    for(int i = 0; E[i] != null; i++){
        for(int r = 0; deleteList[r] != 0; r++){
            int z = deleteList[r];
            E[i].deleteQuestion(z);
        }
    }




    int cnt = 0;
    //输出答案
    while(E[cnt] != null){
        if(E[cnt].getTotalPoint() == 100){
            cnt++;
            continue;
        }else{
            System.out.println("alert: full score of test paper" + E[cnt].getTestNumber() + " is not 100 points");
            cnt++;
        }
    }

    cnt = 1;
    int p = 0;
    //从第一张答卷开始遍历
    for(int i = 0; A[i] != null; i++){
        //判断能否找到试卷
        boolean isExist = false;

        //遍历试卷
        for(p = 0; E[p] != null; p++){
            //若匹配到了试卷
            if(A[i].getAnswerSheetNumber() == E[p].getTestNumber()) {
                //判题
                J[i] = new Judgment(E[p], A[i]);
                J[i].setIsTrueList();
                //用于迭代A[i].getAnswerNumberList()[t]
                int a = 0;
                for (int t = 0; E[p].getQuestionNumberList()[t] != 0; t++) {
                    //题目编号
                    int z = E[p].getQuestionNumberList()[t];

                    //若答案存在
                    boolean isAnswerExist = false;
                    for(a = 0; A[i].getAnswerNumberList()[a] != 0; a++){
                        if(A[i].getAnswerNumberList()[a] == t + 1){
                            isAnswerExist = true;
                            break;
                        }
                    }
                    if (isAnswerExist) {
                        //若题目存在
                        if (E[p].getQuestions()[z] != null) {
                            System.out.print(E[p].getQuestions()[z].getContent());
                            System.out.print("~");
                            System.out.print(A[i].getAnswerList()[a]);
                            System.out.print("~");
                            System.out.println(J[i].getIsTrueList()[a]);
                        } else {
                            //若题目不存在
                            //若题目是被删除的
                            boolean isDelete = false;
                            int test = 0;
                            while(deleteList[test] != 0){
                                if(z == deleteList[test]){
                                    System.out.println("the question " + z + " invalid~0");
                                    isDelete =true;
                                    break;
                                }
                                test++;
                            }
                            //引用题号不存在
                            if(!isDelete){
                                System.out.println("non-existent question~0");
                            }
                        }
                    }else{
                        //答案不存在(优先级最高)
                        System.out.println("answer is null");
                    }
                }

                isExist =true;
                break;
            }else {
                isExist =false;
            }
        }
        //若未找到试卷
        if(!isExist){
            System.out.println("The test paper number does not exist");
            //System.exit(0);
            continue;
        }

        //判分
        //先判断学号是否存在
        boolean isStudentNumber = false;
        for(int temp = 0; S[temp] != null; temp++){
            if(A[i].getStudentNumber().equals(S[temp].getStudentNumber())){
                System.out.print(S[temp].getStudentNumber() + " " + S[temp].getName() + ": ");
                if(E[p] != null) {
                    for (int t = 0; E[p].getQuestionNumberList()[t] != 0; t++) {
                        if (J[i].getIsTrueList()[t]) {
                            System.out.print(E[p].getQuestionScoreList()[t]);
                        } else {
                            System.out.print("0");
                        }
                        if (E[p].getQuestionNumberList()[t + 1] == 0) {
                            System.out.println("~" + J[i].getTotalScore());
                        } else {
                            System.out.print(" ");
                        }
                    }
                }
                isStudentNumber = true;
                break;
            }
            else{
                isStudentNumber = false;
            }
        }
        if(!isStudentNumber){
            System.out.println(A[i].getStudentNumber() + " not found");
        }
    }
}

}
class Question{
private int QuestionNumber;
private String content;
private String standardAnswer;

public Question() {
}

public Question(int questionNumber, String content, String standardAnswer) {
    QuestionNumber = questionNumber;
    this.content = content;
    this.standardAnswer = standardAnswer;
}

public int getQuestionNumber() {
    return QuestionNumber;
}

public void setQuestionNumber(int questionNumber) {
    QuestionNumber = questionNumber;
}

public String getContent() {
    return content;
}

public void setContent(String content) {
    this.content = content;
}

public String getStandardAnswer() {
    return standardAnswer;
}

public void setStandardAnswer(String standardAnswer) {
    this.standardAnswer = standardAnswer;
}

}
class ExaminationPaper{
//下标即为题号
private Question[] questions = new Question[100];
//试卷编号
private int testNumber;
//试卷顺序对应的题号,下标从0开始
private int[] questionNumberList = new int[100];
//试卷顺序对应的题目的分值,下标从0开始
private int[] questionScoreList = new int[100];
private int totalPoint;

public ExaminationPaper() {
}

public Question[] getQuestions() {
    return questions;
}

public void setQuestions(Question[] questions) {
    for(int i = 0; i < questions.length; i++){
        this.questions[i] = questions[i];
    }
}

public int getTestNumber() {
    return testNumber;
}

public void setTestNumber(int testNumber) {
    this.testNumber = testNumber;
}

public int[] getQuestionNumberList() {
    return questionNumberList;
}

public void setQuestionNumberList(int[] questionNumberList) {
    for(int i = 0; i < questionNumberList.length; i++){
        this.questionNumberList[i] = questionNumberList[i];
    }
}

public int[] getQuestionScoreList() {
    return questionScoreList;
}

public void setQuestionScoreList(int[] questionScoreList) {
    for(int i = 0; i < questionScoreList.length; i++){
        this.questionScoreList[i] = questionScoreList[i];
        totalPoint += questionScoreList[i];
    }
}
public int getTotalPoint(){
    return totalPoint;
}
public void deleteQuestion(int number){
    this.questions[number] = null;
}

}

class Student{
private String name;
private String studentNumber;

public Student() {
}

public Student(String name, String studentNumber) {
    this.name = name;
    this.studentNumber = studentNumber;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getStudentNumber() {
    return studentNumber;
}

public void setStudentNumber(String studentNumber) {
    this.studentNumber = studentNumber;
}

}
class AnswerSheet extends Student{
//答卷答案,与试卷题目顺序相同,下标从0开始
private String[] answerList = new String[100];
//试卷编号
private int answerSheetNumber;
//答卷每题的题号
private int[] answerNumberList = new int[100];

public AnswerSheet() {
}

public String[] getAnswerList() {
    return answerList;
}

public void setAnswerList(String[] answerList) {
    for(int i = 0; i < answerList.length; i++){
        this.answerList[i] = answerList[i];
    }
}

public int getAnswerSheetNumber() {
    return answerSheetNumber;
}

public void setAnswerSheetNumber(int answerSheetNumber) {
    this.answerSheetNumber = answerSheetNumber;
}

public int[] getAnswerNumberList() {
    return answerNumberList;
}

public void setAnswerNumberList(int[] answerNumberList) {
    for(int i = 0; i < answerNumberList.length; i++){
        this.answerNumberList[i] = answerNumberList[i];
    }
}

}
class Judgment{
private ExaminationPaper tests;
private AnswerSheet answerSheets;
private int totalScore;
//下标从0开始
private boolean[] isTrueList = new boolean[100];

public Judgment() {
}

public Judgment(ExaminationPaper tests, AnswerSheet answerSheets) {
    this.tests = tests;
    this.answerSheets = answerSheets;
}

public ExaminationPaper getTests() {
    return tests;
}

public void setTests(ExaminationPaper tests) {
    this.tests = tests;
}

public AnswerSheet getAnswerSheets() {
    return answerSheets;
}

public void setAnswerSheets(AnswerSheet answerSheets) {
    this.answerSheets = answerSheets;
}

public boolean[] getIsTrueList() {
    return isTrueList;
}

public void setIsTrueList() {

    for(int i = 0; tests.getQuestionNumberList()[i] != 0; i++){
        //答案题号顺序
        int j = 0;
        while(answerSheets.getAnswerNumberList()[j] != 0){
            if (answerSheets.getAnswerNumberList()[j] == i + 1) {
                //int k = answerSheets.getAnswerNumberList()[i];
                int number = tests.getQuestionNumberList()[i];
                if (tests.getQuestions()[number] != null) {
                    if (answerSheets.getAnswerList()[i].equals(tests.getQuestions()[number].getStandardAnswer())) {
                        isTrueList[i] = true;
                        totalScore += tests.getQuestionScoreList()[i];
                    }
                }
            }
            j++;
        }
    }
}
public int getTotalScore(){
    return totalScore;
}

}

踩坑心得:
1、 跳出舒适圈,勇于学习新知识
这几个作业做下来,相信所有同学都能感受到正则表达式强大的功能,但是相信大家一开始接触正则表达式时都不免被它那奇怪的语法和符号给吓唬到了,认为它很难学,于是还是用自己老一套的循环去分析字符串。事实上,只要克服了一开始调到舒适圈外的恐惧,踏实的学习半个小时正则表达式,就已经可以上手去解决问题了,只是那种陌生感让人感觉不太自信,但只要多尝试几次,就能很快掌握这门强大的工具,大大节省做题时间,是一劳永逸的好事,所以大家不要惧怕新知识,要勇于跳出舒适区,积极去学习新知识。
2、 多写注释
相信大家刚接触编程时或者未接触编程前就早早的了解到如果一段长长的代码没有注释是一件多么痛苦的事。但你亲生体会真正意识到注释的重要性又是什么时候呢?我是这三次作业中才真正意识到了注释的重要性。大家可以看到,我前两次作业的源码注释几乎可以说没有,可能就只有一个总体性的注释,例如什么时候输入,什么时候输入,但这些注释是远远不够的。最好的是能将每一个变量的作用,每一个方法的作用,每一个代码块的作用都要给出注释,因为当代码量大起来后,你可以通过注释去了解过去的自己写这些代码的目的,而不用去逐条逐条的看代码,逐条逐条看代码是非常恐怖了,尤其是在debug的时候,由于作业的周期长,代码的量很大,写到后面你真的会看不懂自己以前写的代码,这会浪费你非常多时间去重新理解你之前的代码,大大降低debug的效率
3、 重视设计
好设计不仅可以让程序的拓展性大大提高,对于目前的我来说,好设计更重要的作用是能把我理清求解问题的思路。并且类图中有各个类的属性和方法,这让你写程序时不必再翻上翻下,只需要看看类图就知道有什么能用,或者有什么类有缺陷。并且设计类的时候不必将心神投入在如何实现上,你将有更多的脑力用于思考如何让类的设计更符合面向对面的设计原则。

改进建议:
我目前的编码类还是太少了,可以将正则表达式匹配输入单独列为一个类。
目前编码拓展性不足,几乎没有使用继承与多态,这个在后续可以加入。

总结:
这次作业收获良多,第一次写这么多行代码的程序,锻炼我的程序设计能力,并且让我意识到了注释的重要性。并且本次作业有不少课外的知识,这还锻炼我的自主学习能力。
建议方面主要是做题的时间太短了,虽然每次作业都有一周的时间,但实际上我们大部分的时间都有课,课外的时间本身就少,全部投入在java中会可能还做不完,但我们其他课程也是有作业需要做,并且数学类课程不进行预习的话上课真的不会有多少收获,我想提的建议是要么将数学课的排课排的更松散一点,要么将java排课排的更松散一点,让我们有时间同时学好数学和java,数学需要时间,java也需要时间,以现在的这种上课速度我真的做不到即学好数学又学好java。

标签:String,答题,int,++,num,判题,三次,new,public
From: https://www.cnblogs.com/yu-shen/p/18149234

相关文章

  • PTA前三次题目集总结
    以下内容是我对PTA三次习题作业最后一题的思路,源码以及总结学到的java知识大多都来自写题目集这些题目对我对java的认知与学习起到了不小的帮助答题判题程序-1题目内容设计实现答题程序,模拟一个小型的测试,要求输入题目信息和答题信息,根据输入题目信息中的标准答案判断答题的......
  • OOP面向对象编程三次题目总结
    一.前言:在这三次题目集中,最后一小题为判题程序,其余题为基础的知识点题,输入一段题目内容,试卷内容,答卷内容,评判对错情况和计算最终总分,在这三次作业的迭代下,我一次比一次做的不好,第一次是满分,第二次95,第三次只有79,即使效果不好,我还是把我的做题思路写下来供大家参考。二.三次作业......
  • PTA前三次题目集总结
    PTA前三次题目集总结1.前言:2.设计与分析3.踩坑心得4.改进建议5.总结1.前言:  1.前三次题目集的重心从对类的使用和设计,类的关系,到类的封装。第一次题目集题目较多,主要是为了让作为初学者的我们能掌握Java的基本操作。第二次题目集题量适中,但增加了使用链表,自定义类要实现......
  • 第一次Blog总结-前三次答题判定程序
    目录前言设计与分析采坑心得改进建议总结关于java&面向对象因为这学期刚开始学习java,因此对于java的某些知识点或技巧掌握地不是太熟练,但是经过长达三周的折磨下,总算对java有了初步的总体认知。前言知识点覆盖:数组灵活运用,List和Map接口灵活运用,各种类的设计已经......
  • 答题判题程序1~3总结
    一.前言第一次作业知识点、题量、难度总结1.第一次作业的主要内容为:输入题目信息和答题信息,根据输入题目信息中的标准答案判断答题的结果。其中输入的信息主要为:题目数量,题目内容,答题信息,以"end"结束输入。要求输出答题信息和判题信息。2.主要涉及到的知识点:(1)面向对象编程:......
  • 三次答题判题程序练习让你入门Java。
    (1)前言本阶段三次题目集涵盖了从基础编程概念到较复杂算法设计等多个知识点。题量适中,难度呈梯度上升,从简单的数据结构与算法实现到复杂的问题求解,逐步挑战学生的编程能力。第一次题目集主要考察基本语法、数据类型和简单的控制结构;第二次题目集则增加了数组、链表等数据结构的应......
  • PTA三次题目集总结性Blog
    (1)前言1.第一次题目集知识点主要涉及类的设计和实现、构造方法的使用、成员变量和访问器/修改器的定义、方法的重写(toString方法)、对象的创建和调用方法、控制台输入和输出的使用、浮点数的格式化输出等,题量比较适中,难度也比较适中,要求实现的功能较为基础,但需要一定的类设计和方......
  • NTT 的三次变八次优化
    就是将NTT的域扩到复数。我不知道高斯整数的其他取模方式,所以巨慢/xk。而且没用。我们知道高斯整数的神秘取模方式,使得其结果的范数小于除数的一半。然而我们发现这个取模在整数下的结果仍然在整数取模的同余系中!好很有精神!我们选取\(g=3,mod=998244353\),我们只需要验证\(......
  • 【2024年5月备考新增】《软考案例分析答题技巧(3)质量、资源》
    2.5项目质量管理质量管理过程质量管理过程:规划质量管理-管理质量-控制质量。管理质量意义:①通过执行有关产品特定方面的设计准则,设计出最优的成熟产品;②建立信心,相信通过质量保证工具和技术(如质量审计和故障分析)可以使未来输出在完工时满足特定的需求和期望;③确......
  • 【2024年5月备考新增】《软考案例分析答题技巧(4)沟通、干系人、风险》
    2.7项目沟通管理项目沟通管理过程:规划沟通管理-管理沟通-监督沟通。沟通5种基本状态:已发送、已收到、已理解、已认可、已转化为积极行动。沟通分类:内部沟通、外部沟通、正式沟通、非正式沟通、官方沟通、非官方沟通、书面与口头沟通。沟通技巧:书面沟通(5C:正确、简洁、......