关于pta三次作业的总结

oop三次pta总结

前言

在这学期的java课程学习当中,我已经体会到了java这门语言的重要性了,就从这三次pta题目的设计与思路来说吧(还真的有点小难),特别是每一期pta的最后一题......

答题判题程序》,这道题目依次迭代,难度依次上升(如果没有设计好,基本是寄了),题目不多看几遍细节是真的不能把握,如果细节忽略了,那就要付出几倍的时间和

去修正了。(别问我怎么知道的)

这道题目涵盖正则表达式类的设计,等知识点。从复杂到更复杂,时间耗费的越来越多,(一个测试点一天)

接下来一题一题来分析看看吧,也总结一下自己的代码设计错误

 

设计与分析

题目1:

设计实现答题程序,模拟一个小型的测试,要求输入题目信息和答题信息,根据输入题目信息中的标准答案判断答题的结果。

输入格式:

程序输入信息分三部分:


1、题目数量

   格式:整数数值,若超过1位最高位不能为0,

   样例:34

2、题目内容

   一行为一道题,可以输入多行数据。

   格式:"#N:"+题号+" "+"#Q:"+题目内容+" "#A:"+标准答案

格式约束:题目的输入顺序与题号不相关,不一定按题号顺序从小到大输入。

   样例:#N:1 #Q:1+1= #A:2

         #N:2 #Q:2+2= #A:4

3、答题信息

   答题信息按行输入,每一行为一组答案,每组答案包含第2部分所有题目的解题答案,答案的顺序号与题目题号相对应。

   格式:"#A:"+答案内容

   格式约束:答案数量与第2部分题目的数量相同,答案之间以英文空格分隔。

   样例:#A:2 #A:78

      2是题号为1的题目的答案
      78是题号为2的题目的答案

   答题信息以一行"end"标记结束,"end"之后的信息忽略。

输出格式:

1、题目数量

   格式:整数数值,若超过1位最高位不能为0,

   样例:34

2、答题信息

   一行为一道题的答题信息,根据题目的数量输出多行数据。

   格式:题目内容+" ~"+答案

   样例:1+1=~2

          2+2= ~4

3、判题信息

   判题信息为一行数据,一条答题记录每个答案的判断结果,答案的先后顺序与题目题号相对应。

   格式:判题结果+" "+判题结果

   格式约束:

     1、判题结果输出只能是true或者false,
     2、判题信息的顺序与输入答题信息中的顺序相同

   样例:true false true

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int number;
        number = sc.nextInt();
        Answerpaper answerpaper[] = new Answerpaper[2 * number + 1];
        int[] num = new int[number + 1];
        String str;
        sc.nextLine();
        String[][] Str = new String[number + 2][];
        for (int i = 1; i <= number + 1; i++) {
            str = sc.nextLine();
            Str[i] = str.split(":");
            for (int j = 1; j < Str[i].length; j++) {
                while (Str[i][j].charAt(0) == ' ') {
                    Str[i][j] = Str[i][j].substring(1);
                }
                if (i == number + 1) {
                    while (Str[i][j].charAt(Str[i][j].length() - 1) == '#' ||
                            Str[i][j].charAt(Str[i][j].length() - 1) == 'A' ||
                            Str[i][j].charAt(Str[i][j].length() - 1) == ' ') {
                        Str[i][j] = Str[i][j].substring(0, Str[i][j].length() - 1);
                    }
                }
                if (j == 1 && i != number + 1) {
                    while (Str[i][j].charAt(Str[i][j].length() - 1) == '#' ||
                            Str[i][j].charAt(Str[i][j].length() - 1) == 'Q' ||
                            Str[i][j].charAt(Str[i][j].length() - 1) == ' ') {
                        Str[i][j] = Str[i][j].substring(0, Str[i][j].length() - 1);
                    }
                    num[i] = 0;
                    for (int k = 0; k < Str[i][j].length(); k++)
                        num[i] += ((int) Str[i][j].charAt(k) - 48) * Math.pow(10, Str[i][j].length() - k - 1);

                }
                if (j == 2 && i != number + 1) {
                    Str[i][j] = Str[i][j].substring(0, Str[i][j].length() - 1);
                    Str[i][j] = Str[i][j].substring(0, Str[i][j].length() - 1);
                    while (Str[i][j].charAt(Str[i][j].length() - 1) == ' ') {
                        Str[i][j] = Str[i][j].substring(0, Str[i][j].length() - 1);
                    }
                }
                if (j == 3) {
                    while (Str[i][j].charAt(Str[i][j].length() - 1) == ' ') {
                        Str[i][j] = Str[i][j].substring(0, Str[i][j].length() - 1);
                    }
                }
            }

        }

        for (int i = 1; i <= number; i++) {
            answerpaper[num[i]] = new Answerpaper(number, num[i], Str[1 + number][num[i]], Str[i][3], Str[i][2]);
        }
        for (int i = 1; i <= number; i++) {
            for (int j = 1; j <= number - 1; j++) {
                if (num[j] > num[j + 1]) {
                    int a = num[j + 1];
                    num[j + 1] = num[j];
                    num[j] = a;
                }
            }
        }
        for (int i = 1; i <= number; i++) {
            answerpaper[num[i]].printlnof();
            answerpaper[num[i]].printresult();
        }
        for (int i = 1; i <= number; i++) {
            if (answerpaper[num[i]].paper.problem.answerright(answerpaper[num[i]])) {
                if (i != number) {
                    System.out.printf("true ");
                } else {
                    System.out.printf("true");
                }

            } else {
                if (i != number)
                    System.out.printf("false ");
                else
                    System.out.printf("false");
            }
        }
    }
}

class Problem {
    int num;
    String content;
    String standardAnswer;

    public Problem(int num, String content, String standardAnswer) {
        this.num = num;
        this.content = content;
        this.standardAnswer = standardAnswer;
    }

    public boolean answerright(Answerpaper answerpaper) {
        if (answerpaper.answer.equals(standardAnswer))
            return true;
        return false;
    }
}

class Paper {
    Problem problem;
    int number;

    public Paper(int number, String standardAnswer, int num, String content) {
        problem = new Problem(num, content, standardAnswer);
        this.number = number;
    }

    public Problem getProblem() {
        return this.problem;
    }
}

class Answerpaper {
    Paper paper;
    static int num = 0;
    String answer;
    private boolean result;

    public Answerpaper(int number, int num, String standardAnswer, String answer, String content) {
        this.answer = answer;
        paper = new Paper(number, standardAnswer, num, content);
    }

    public boolean numjudgeAnswer(int num) {
        return true;
    }

    public void printlnof() {
        System.out.printf("%s", paper.getProblem().content);
    }

    public void printresult() {
        System.out.printf("~%s\n", paper.getProblem().standardAnswer);
    }
}
    

 

 我的基本思路是将输入的问题,试卷,答卷分割成各个部分,并储存进Answerpaper类。Answerpaper类中有paper类,paper类中有problem类储存题目,这样一来就可通过引用答卷类从而对答卷上的答题信息进行正误的判断,将三种信息很好的关联了起来,我为了分割字符串还在"main"函数中定义了一个二维字符串数组。。。而我分割输入就是通过"N","S","T"来判断输入是问题还是试卷还是答卷。再使用split()和substring函数从而做到将多余的字符去掉从而得到需要的

看上去我对类的设计非常不错,三个类分的很好,但是但是我类中的变量都不是private(私有的),从而很严重的破坏了类的封装性。主要是当时也为了图方便,想着直接引用就行了,还不用写getter方法 ,但是却忘了最重要的类的封装性。

还有就是我在主方法里面分割字符串,主方法里面就只能做两件事就是输入输出,但是我却在主方法里面做了别的事,这是不可取得

然后就是我没有用正则表达式(最简便得方法),正则表达式我竟然学了没用,本来这道题就是考察正则表达式的。叹。。。(无力感)

 

题目2:

设计实现答题程序,模拟一个小型的测试,以下粗体字显示的是在答题判题程序-1基础上增补或者修改的内容。

要求输入题目信息、试卷信息和答题信息,根据输入题目信息中的标准答案判断答题的结果。

输入格式:

程序输入信息分三种,三种信息可能会打乱顺序混合输入:

1、题目信息

   一行为一道题,可输入多行数据(多道题)。

   格式:"#N:"+题目编号+" "+"#Q:"+题目内容+" "#A:"+标准答案

格式约束:

    1、题目的输入顺序与题号不相关,不一定按题号顺序从小到大输入。
    2、允许题目编号有缺失,例如:所有输入的题号为1、2、5,缺少其中的3号题。此种情况视为正常。

   样例:#N:1 #Q:1+1= #A:2

         #N:2 #Q:2+2= #A:4

2、试卷信息

一行为一张试卷,可输入多行数据(多张卷)。

格式:"#T:"+试卷号+" "+题目编号+"-"+题目分值

     题目编号应与题目信息中的编号对应。

     一行信息中可有多项题目编号与分值。

样例:#T:1 3-5 4-8 5-2

3、答卷信息

    答卷信息按行输入,每一行为一张答卷的答案,每组答案包含某个试卷信息中的题目的解题答案,答案的顺序与试卷信息中的题目顺序相对应。

   格式:"#S:"+试卷号+" "+"#A:"+答案内容

   格式约束:答案数量可以不等于试卷信息中题目的数量,没有答案的题目计0分,多余的答案直接忽略,答案之间以英文空格分隔。

   样例:#S:1 #A:5 #A:22

       1是试卷号 

       5是1号试卷的顺序第1题的题目答案

       22是1号试卷的顺序第2题的题目答案

   答题信息以一行"end"标记结束,"end"之后的信息忽略。

输出格式:

1、试卷总分警示

该部分仅当一张试卷的总分分值不等于100分时作提示之用,试卷依然属于正常试卷,可用于后面的答题。如果总分等于100分,该部分忽略,不输出。

   格式:"alert: full score of test paper"+试卷号+" is not 100 points"

   样例:alert: full score of test paper2 is not 100 points

2、答卷信息

   一行为一道题的答题信息,根据试卷的题目的数量输出多行数据。

   格式:题目内容+"~"+答案++"~"+判题结果(true/false)

约束:如果输入的答案信息少于试卷的题目数量,答案的题目要输"answer is null"   

样例:   3+2=~5~true

         4+6=~22~false.

      answer is null

3、判分信息

   判分信息为一行数据,是一条答题记录所对应试卷的每道小题的计分以及总分,计分输出的先后顺序与题目题号相对应。

   格式:题目得分+" "+....+题目得分+"~"+总分

   格式约束:

 1、没有输入答案的题目计0分

 2、判题信息的顺序与输入答题信息中的顺序相同
 

   样例:5 8 0~13

根据输入的答卷的数量以上2、3项答卷信息与判分信息将重复输出。

4、提示错误的试卷号

如果答案信息中试卷的编号找不到,则输出”the test paper number does not exist”,参见样例9。

 

设计建议:

参考答题判题程序-1,建议增加答题类,类的内容以及类之间的关联自行设计。

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Question question = new Question();
        ScoreSumJudge scoresumjudge = new ScoreSumJudge();
        AnswerQuestion answerquestion = new AnswerQuestion();
        SplitInput splitinput = new SplitInput();
        int QuestionSum = 0, PaperSum = 0, AnswerSum = 0;
        String input[] = new String[100];
        for (int i = 1; i < i + 1; i++) {
            input[i] = sc.nextLine();
            if (input[i].equals("end"))
                break;
        }
        int i = 1;
        while (!(input[i].equals("end"))) {
            splitinput.setInput(input[i]);
            if (input[i].charAt(1) == 'N') {
                splitinput.splitQuestion();
                if (splitinput.getQuestionNum() > QuestionSum) {
                    QuestionSum = splitinput.getQuestionNum();
                }
                question.setQuestion(QuestionSum, splitinput.getQuestionNum(), splitinput.getQuestion(), splitinput.getStandardAnswer());

            }
            i++;
        }
        i = 1;
        while (!(input[i].equals("end"))) {
            splitinput.setInput(input[i]);
            if (input[i].charAt(1) == 'T') {
                splitinput.splitPaper();
                if (splitinput.getPaperNum() > PaperSum) {
                    PaperSum = splitinput.getPaperNum();
                }
                scoresumjudge.setScore(PaperSum, splitinput.getPaperNum(), splitinput.getPaperQuestionNum(), splitinput.getPaperQuestionMark());
                answerquestion.setPaper(PaperSum, splitinput.getPaperNum());
                answerquestion.getPaper()[splitinput.getPaperNum()].setPaper(splitinput.getPaperQuestionSum(), splitinput.getPaperQuestionNum(), splitinput.getPaperQuestionMark(), question);
            }
            i++;
        }
        scoresumjudge.printResult();
        i = 1;
        while (!(input[i].equals("end"))) {
            splitinput.setInput(input[i]);
            if (input[i].charAt(1) == 'S') {
                splitinput.splitAnswer();
                if (splitinput.getAnswerPaperNum() > AnswerSum) {
                    AnswerSum = splitinput.getAnswerPaperNum();
                }
                answerquestion.setAnswerPaper(AnswerSum, splitinput.getAnswerPaperNum());
                answerquestion.getAnswerPaper()[splitinput.getAnswerPaperNum()].setAnswerPaper(splitinput.getAnswer());
                for (int k = 1; k <= answerquestion.getPaperSum(); k++) {
                    if (k == splitinput.getAnswerPaperNum()) {
                        answerquestion.judge(splitinput.getAnswerPaperNum(), question);
                        break;
                    }
                    if (k == answerquestion.getPaperSum()) {
                        System.out.println("The test paper number does not exist");
                        break;
                    }
                }
            }
            i++;
        }
    }
}

class judge {
    AnswerQuestion answerQuestion = new AnswerQuestion();
    Question question = new Question();

    public judge() {

    }

    public judge(AnswerQuestion answerQuestion, Question question) {
        this.answerQuestion = answerQuestion;
        this.question = question;
    }

}

class SplitInput {
    private String Question;
    private String QuestionNum;
    private String StandardAnswer;
    private String Questionmark;
    private String input;
    private int PaperQuestionSum;
    private int AnswerPaperNum;
    private String[] Answer;
    private String PaperNum;
    private int[] PaperQuestionNum;
    private int[] PaperQuestionMark;

    public SplitInput() {

    }

    public SplitInput(String input) {
        this.input = input;
    }

    public void setInput(String input) {
        this.input = input;
    }

    public void splitQuestion() {
        input = input.replaceAll(" #A", "");
        String s[] = input.split(":");
        while (s[1].charAt(s[1].length() - 1) == ' ' || s[1].charAt(s[1].length() - 1) == '#' || s[1].charAt(s[1].length() - 1) == 'Q') {
            s[1] = s[1].substring(0, s[1].length() - 1);
        }
        while (s[2].charAt(s[2].length() - 1) == ' ' || s[2].charAt(s[2].length() - 1) == '#' || s[2].charAt(s[2].length() - 1) == 'A') {
            s[2] = s[2].substring(0, s[2].length() - 1);
        }
        this.QuestionNum = s[1];
        this.Question = s[2];
        this.StandardAnswer = s[3];
    }

    public void splitPaper() {
        input = input.replaceAll("#T:", "");
        input = input.replaceAll("#A:", "");
        input = input.replaceAll("#Q:", "");
        String[] s = input.split("\\s+");
        PaperQuestionNum = new int[s.length];
        PaperQuestionMark = new int[s.length];
        PaperNum = s[0];
        PaperQuestionSum = s.length - 1;
        for (int i = 1; i < s.length; i++) {
            String[] ss = s[i].split("-");
            PaperQuestionNum[i] = Integer.parseInt(ss[0]);
            PaperQuestionMark[i] = Integer.parseInt(ss[1]);
        }
    }

    public void splitAnswer() {
        input = input.replaceAll("#S:", "");
        input = input.replaceAll("#A:", "");
        String[] s = input.split("\\s+");
        this.AnswerPaperNum = Integer.parseInt(s[0]);
        this.Answer = new String[s.length];
        for (int i = 1; i < s.length; i++) {
            this.Answer[i] = s[i];
        }
    }

    public int getQuestionNum() {
        return Integer.parseInt(this.QuestionNum);
    }

    public String getQuestion() {
        return this.Question;
    }

    public String getStandardAnswer() {
        return this.StandardAnswer;
    }

    public int getPaperQuestionSum() {
        return PaperQuestionSum;
    }

    public int getPaperNum() {
        return Integer.parseInt(this.PaperNum);
    }

    public int[] getPaperQuestionNum() {
        return this.PaperQuestionNum;
    }

    public int[] getPaperQuestionMark() {
        return this.PaperQuestionMark;
    }

    public int getAnswerPaperNum() {
        return this.AnswerPaperNum;
    }

    public String[] getAnswer() {
        return this.Answer;
    }
}

class ScoreSumJudge {
    private int MarkSum;
    private int PaperSum = 0;
    private String result[] = new String[PaperSum + 1];
    private int PaperQuestionNum[] = new int[PaperSum + 1];
    private int score[] = new int[PaperSum + 1];

    public ScoreSumJudge() {

    }

    public void upDate() {
        String result2[] = new String[PaperSum + 1];
        for (int i = 1; i < this.result.length; i++) {
            result2[i] = result[i];
        }
        result = new String[PaperSum];
        result = result2;
    }

    public void setScore(int PaperSum, int PaperNum, int[] PaperQuestionNum, int[] PaperQuestionMark) {
        this.PaperSum = PaperSum;
        this.MarkSum = 0;
        this.upDate();
        for (int i = 1; i < PaperQuestionNum.length; i++) {
            MarkSum += PaperQuestionMark[i];
        }
        if (MarkSum != 100)
            result[PaperNum] = "alert: full score of test paper" + PaperNum + " is not 100 points";
        else
            result[PaperNum] = "";
    }

    public void printResult() {
        for (int i = 1; i < result.length; i++) {
            if (!(result[i].equals(""))) {
                System.out.println(result[i]);
            }
        }
    }
}

class AnswerQuestion {
    private int AnswerSum = 0;
    private int PaperSum = 0;
    AnswerPaper answerPaper[] = new AnswerPaper[AnswerSum + 1];
    Paper paper[] = new Paper[PaperSum + 1];

    public AnswerQuestion() {

    }

    public int getPaperSum() {
        return this.PaperSum;
    }

    public int getAnswerSum() {
        return this.PaperSum;
    }

    public void upDatePaper() {
        Paper paper2[] = new Paper[PaperSum + 1];
        for (int i = 1; i < paper.length; i++) {
            paper2[i] = paper[i];
        }
        this.paper = new Paper[PaperSum + 1];
        this.paper = paper2;
    }

    public void upDateAnswerPaper() {
        AnswerPaper answerPaper2[] = new AnswerPaper[AnswerSum + 1];
        for (int i = 1; i < answerPaper.length; i++) {
            answerPaper2[i] = answerPaper[i];
        }
        answerPaper = new AnswerPaper[AnswerSum + 1];
        answerPaper = answerPaper2;
    }

    public void setPaper(int PaperSum, int PaperNum) {
        this.PaperSum = PaperSum;
        this.upDatePaper();
        paper[PaperNum] = new Paper();
    }

    public void judge(int AnswerNum, Question question) {
        int mark = 0;
        if (this.getAnswerPaper()[AnswerNum].getAnswer().length - 1 == this.getPaper()[AnswerNum].getPaperSum()) {
            for (int i = 1; i <= this.getPaper()[AnswerNum].getPaperSum(); i++) {
                if (this.getAnswerPaper()[AnswerNum].getAnswer()[i].equals(question.getStandardAnswer()[this.getPaper()[AnswerNum].getPaperQuestionNum()[i]])) {
                    System.out.println(question.getQuestion()[this.getPaper()[AnswerNum].getPaperQuestionNum()[i]] + "~" + this.getAnswerPaper()[AnswerNum].getAnswer()[i] + "~" + "true");
                    mark += this.getPaper()[AnswerNum].getPaperQuestionMark()[i];
                } else {
                    System.out.println(question.getQuestion()[this.getPaper()[AnswerNum].getPaperQuestionNum()[i]] + "~" + this.getAnswerPaper()[AnswerNum].getAnswer()[i] + "~" + "false");

                }
            }
            for (int i = 1; i <= this.getPaper()[AnswerNum].getPaperSum(); i++) {
                if (this.getAnswerPaper()[AnswerNum].getAnswer()[i].equals(question.getStandardAnswer()[this.getPaper()[AnswerNum].getPaperQuestionNum()[i]])) {
                    if (i == this.getPaper()[AnswerNum].getPaperSum())
                        System.out.printf("%s~", this.getPaper()[AnswerNum].getPaperQuestionMark()[i]);
                    else
                        System.out.printf("%s ", this.getPaper()[AnswerNum].getPaperQuestionMark()[i]);
                } else {
                    if (i == this.getPaper()[AnswerNum].getPaperSum())
                        System.out.printf("0~");
                    else
                        System.out.printf("0 ");
                }

            }
            System.out.printf("%d\n", mark);
        }
        if (this.getAnswerPaper()[AnswerNum].getAnswer().length - 1 != this.getPaper()[AnswerNum].getPaperSum()) {
            for (int i = 1; i <= this.getAnswerPaper()[AnswerNum].getAnswer().length - 1; i++) {
                if (this.getAnswerPaper()[AnswerNum].getAnswer()[i].equals(question.getStandardAnswer()[this.getPaper()[AnswerNum].getPaperQuestionNum()[i]])) {
                    System.out.println(question.getQuestion()[this.getPaper()[AnswerNum].getPaperQuestionNum()[i]] + "~" + this.getAnswerPaper()[AnswerNum].getAnswer()[i] + "~" + "true");
                    mark += this.getPaper()[AnswerNum].getPaperQuestionMark()[i];
                } else {
                    System.out.println(question.getQuestion()[this.getPaper()[AnswerNum].getPaperQuestionNum()[i]] + "~" + this.getAnswerPaper()[AnswerNum].getAnswer()[i] + "~" + "false");

                }
            }
            for (int i = this.getAnswerPaper()[AnswerNum].getAnswer().length; i <= this.getPaper()[AnswerNum].getPaperSum(); i++) {
                System.out.println("answer is null");
            }
            for (int i = 1; i <= this.getAnswerPaper()[AnswerNum].getAnswer().length - 1; i++) {
                if (this.getAnswerPaper()[AnswerNum].getAnswer()[i].equals(question.getStandardAnswer()[this.getPaper()[AnswerNum].getPaperQuestionNum()[i]])) {
                    if (i == this.getPaper()[AnswerNum].getPaperSum())
                        System.out.printf("%s ", this.getPaper()[AnswerNum].getPaperQuestionMark()[i]);
                    else
                        System.out.printf("%s ", this.getPaper()[AnswerNum].getPaperQuestionMark()[i]);
                }
                if (!(this.getAnswerPaper()[AnswerNum].getAnswer()[i].equals(question.getStandardAnswer()[this.getPaper()[AnswerNum].getPaperQuestionNum()[i]]))) {
                    System.out.printf("0 ");
                }
            }
            for (int i = this.getAnswerPaper()[AnswerNum].getAnswer().length; i <= this.getPaper()[AnswerNum].getPaperSum(); i++) {
                if (i == this.getPaper()[AnswerNum].getPaperSum()) {
                    System.out.printf("0~");
                } else
                    System.out.printf("0 ");
            }
            System.out.printf("%d\n", mark);
        }
    }

    public Paper[] getPaper() {
        return this.paper;
    }

    public AnswerPaper[] getAnswerPaper() {
        return this.answerPaper;
    }

    public void setAnswerPaper(int AnswerSum, int AnswerNum) {
        this.AnswerSum = AnswerSum;
        this.upDateAnswerPaper();
        answerPaper[AnswerNum] = new AnswerPaper();
    }

    public void judge() {

    }
}

class AnswerPaper {
    private int AnswerPaperNum;
    private String[] Answer;

    public AnswerPaper() {

    }

    public AnswerPaper(int AnswerPaperNum, String[] Answer) {
        this.AnswerPaperNum = AnswerPaperNum;
        this.Answer = Answer;
    }

    public void setAnswerPaper(String[] Answer) {
        this.Answer = Answer;
    }

    public String[] getAnswer() {
        return this.Answer;
    }
}

class Paper {
    private int PaperSum = 0;  //试卷题目数量;
    private int PaperQuestionNum[] = new int[PaperSum + 1];//题目标号
    private int PaperQuestionMark[] = new int[PaperSum + 1];//编号下对应分数
    private Question question = new Question();

    public Paper() {
    }

    public int[] getPaperQuestionNum() {
        return this.PaperQuestionNum;
    }

    public int[] getPaperQuestionMark() {
        return this.PaperQuestionMark;
    }

    public int getPaperSum() {
        return this.PaperSum;
    }

    public Paper(int PaperSum, int[] PaperQuestionNum, int PaperQuestionMark[], Question question) {
        this.PaperSum = PaperSum;
        this.PaperQuestionNum = new int[PaperSum + 1];
        this.PaperQuestionMark = new int[PaperSum + 1];
        this.PaperQuestionNum = PaperQuestionNum;
        this.PaperQuestionMark = PaperQuestionMark;
    }

    public void setPaper(int PaperSum, int[] PaperQuestionNum, int PaperQuestionMark[], Question question) {
        this.PaperSum = PaperSum;
        this.PaperQuestionNum = new int[PaperSum + 1];
        this.PaperQuestionMark = new int[PaperSum + 1];
        this.PaperQuestionNum = PaperQuestionNum;
        this.PaperQuestionMark = PaperQuestionMark;
    }
}

class Question {
    private int QuestionSum = 0;
    private String Question[] = new String[QuestionSum + 1];
    public String StandardAnswer[] = new String[QuestionSum + 1];

    public Question() {
    }

    public void upDate() {
        String Question2[] = new String[this.QuestionSum + 1];
        String StandardAnswer2[] = new String[this.QuestionSum + 1];
        for (int i = 1; i < Question.length; i++) {
            Question2[i] = Question[i];
            StandardAnswer2[i] = StandardAnswer[i];
        }
        this.Question = new String[this.QuestionSum + 1];
        this.StandardAnswer = new String[this.QuestionSum + 1];
        Question = Question2;
        StandardAnswer = StandardAnswer2;
    }

    public void setQuestion(int QuestionSum, int QuestionNum, String Question, String StandardAnswer) {
        this.QuestionSum = QuestionSum;
        this.upDate();
        this.Question[QuestionNum] = Question;
        this.StandardAnswer[QuestionNum] = StandardAnswer;
    }

    public String[] getQuestion() {
        return this.Question;
    }

    public String[] getStandardAnswer() {
        return this.StandardAnswer;
    }
}

 

 

 

相较于第一题,这一题难度直接上来了,增加了太多东西了。(难)

增加了答卷,判定试卷是不是满分,还有不一样的判分输出,乱序输入.........

这道题真的就要好好地看题目,要去把每一个细节抓住才可以下笔,而我因为上一次写的很不好需要重新构造类(重写了一天)

我的基本思路是将输入通过splitinput类进行分割,将所有的题目储存进入question类 ,所有的试卷存入试卷类,然后根据ScoreSumJudge类判断试卷是否是满分试卷。而答卷类通过输入去遍历,从而完成对答卷分数结果的输出

这道题的不足就在于对答卷结果的分析是在AnswerQuestion类中,没有做到类的单一职责性

 

题目3:

设计实现答题程序,模拟一个小型的测试,以下粗体字显示的是在答题判题程序-2基础上增补或者修改的内容,要求输入题目信息、试卷信息、答题信息、学生信息、删除题目信息,根据输入题目信息中的标准答案判断答题的结果。

输入格式:

程序输入信息分五种,信息可能会打乱顺序混合输入。

1、题目信息
题目信息为独行输入,一行为一道题,多道题可分多行输入。

格式:"#N:"+题目编号+" "+"#Q:"+题目内容+" "#A:"+标准答案

格式约束:
    1、题目的输入顺序与题号不相关,不一定按题号顺序从小到大输入。
    2、允许题目编号有缺失,例如:所有输入的题号为1、2、5,缺少其中的3号题。此种情况视为正常。
样例:#N:1 #Q:1+1= #A:2
     #N:2 #Q:2+2= #A:4

     
2、试卷信息

  试卷信息为独行输入,一行为一张试卷,多张卷可分多行输入数据。
格式:"#T:"+试卷号+" "+题目编号+"-"+题目分值+" "+题目编号+"-"+题目分值+...

格式约束:
   题目编号应与题目信息中的编号对应。
   一行信息中可有多项题目编号与分值。 
样例:#T:1 3-5 4-8 5-2   

     
3、学生信息

  学生信息只输入一行,一行中包括所有学生的信息,每个学生的信息包括学号和姓名,格式如下。

格式:"#X:"+学号+" "+姓名+"-"+学号+" "+姓名....+"-"+学号+" "+姓名     

格式约束:
    答案数量可以不等于试卷信息中题目的数量,没有答案的题目计0分,多余的答案直接忽略,答案之间以英文空格分隔。
 样例:
       #S:1 #A:5 #A:22
       1是试卷号 
       5是1号试卷的顺序第1题的题目答案 

4、答卷信息

  答卷信息按行输入,每一行为一张答卷的答案,每组答案包含某个试卷信息中的题目的解题答案,答案的顺序号与试 卷信息中的题目顺序相对应。答卷中:

格式:"#S:"+试卷号+" "+学号+" "+"#A:"+试卷题目的顺序号+"-"+答案内容+...

    

格式约束:
       答案数量可以不等于试卷信息中题目的数量,没有答案的题目计0分,多余的答案直接忽略,答案之间以英文空格分隔。
       答案内容可以为空,即””。
       答案内容中如果首尾有多余的空格,应去除后再进行判断。
样例:
       #T:1 1-5 3-2 2-5 6-9 4-10 7-3
       #S:1 20201103 #A:2-5 #A:6-4
       1是试卷号
       20201103是学号
       2-5中的2是试卷中顺序号,5是试卷第2题的答案,即T中3-2的答案 
       6-4中的6是试卷中顺序号,4是试卷第6题的答案,即T中7-3的答案 
注意:不要混淆顺序号与题号

 

5、删除题目信息

  删除题目信息为独行输入,每一行为一条删除信息,多条删除信息可分多行输入。该信息用于删除一道题目信息,题目被删除之后,引用该题目的试卷依然有效,但被删除的题目将以0分计,同时在输出答案时,题目内容与答案改为一条失效提示,例如:”the question 2 invalid~0”

格式:"#D:N-"+题目号

格式约束:

       题目号与第一项”题目信息”中的题号相对应,不是试卷中的题目顺序号。

       本题暂不考虑删除的题号不存在的情况。      
样例:
#N:1 #Q:1+1= #A:2
#N:2 #Q:2+2= #A:4
#T:1 1-5 2-8
#X:20201103 Tom-20201104 Jack
#S:1 20201103 #A:1-5 #A:2-4
#D:N-2
end

输出
alert: full score of test paper1 is not 100 points
1+1=~5~false
the question 2 invalid~0
20201103 Tom: 0 0~0

     答题信息以一行"end"标记结束,"end"之后的信息忽略。

输出格式:


1、试卷总分警示


该部分仅当一张试卷的总分分值不等于100分时作提示之用,试卷依然属于正常试卷,可用于后面的答题。如果总分等于100 分,该部分忽略,不输出。


格式:"alert: full score of test paper"+试卷号+" is not 100 points"

 样例:alert: full score of test paper2 is not 100 points


2、答卷信息


一行为一道题的答题信息,根据试卷的题目的数量输出多行数据。

格式:题目内容+"~"+答案++"~"+判题结果(true/false)

约束:如果输入的答案信息少于试卷的题目数量,答案的题目要输"answer is null"   
样例:
     3+2=~5~true
     4+6=~22~false.
     answer is null

     

3、判分信息

 判分信息为一行数据,是一条答题记录所对应试卷的每道小题的计分以及总分,计分输出的先后顺序与题目题号相对应。

    格式:**学号+" "+姓名+": "**+题目得分+" "+....+题目得分+"~"+总分

    格式约束:

     1、没有输入答案的题目、被删除的题目、答案错误的题目计0分
     2、判题信息的顺序与输入答题信息中的顺序相同

    样例:20201103 Tom: 0 0~0

       根据输入的答卷的数量以上2、3项答卷信息与判分信息将重复输出。

 
4、被删除的题目提示信息


当某题目被试卷引用,同时被删除时,答案中输出提示信息。样例见第5种输入信息“删除题目信息”。


5、题目引用错误提示信息


试卷错误地引用了一道不存在题号的试题,在输出学生答案时,提示”non-existent question~”加答案。例如:

输入:

#N:1 #Q:1+1= #A:2
#T:1 3-8
#X:20201103 Tom-20201104 Jack-20201105 Www
#S:1 20201103 #A:1-4
end

输出:
alert: full score of test paper1 is not 100 points
non-existent question~0
20201103 Tom: 0~0
如果答案输出时,一道题目同时出现答案不存在、引用错误题号、题目被删除,只提示一种信息,答案不存在的优先级最高,例如:
输入:
#N:1 #Q:1+1= #A:2
#T:1 3-8
#X:20201103 Tom-20201104 Jack-20201105 Www
#S:1 20201103
end

输出:
alert: full score of test paper1 is not 100 points
answer is null
20201103 Tom: 0~0


6、格式错误提示信息


输入信息只要不符合格式要求,均输出”wrong format:”+信息内容。

      例如:wrong format:2 #Q:2+2= #4

7、试卷号引用错误提示输出

 

如果答卷信息中试卷的编号找不到,则输出”the test paper number does not exist”,答卷中的答案不用输出,参见样例8。

 

8、学号引用错误提示信息


如果答卷中的学号信息不在学生列表中,答案照常输出,判分时提示错误。参见样例9。

 

本题暂不考虑出现多张答卷的信息的情况。

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Question question = new Question();
        ScoreSumJudge scoresumjudge = new ScoreSumJudge();
        AnswerQuestion answerquestion = new AnswerQuestion();
        StudentId studentId = new StudentId();
        SplitInput splitinput = new SplitInput();
        JudgeWrongFormat judgeWrongFormat = new JudgeWrongFormat();
        SingleInformationInput single = new SingleInformationInput();
        JudgeWrongFormat2 j = new JudgeWrongFormat2();
        Judge judge = new Judge();
        Agent agent = new Agent();
        Delete delete = new Delete();
        int QuestionSum = 0, PaperSum = 0, AnswerSum = 0, l = 0;
        String input[] = new String[100];
        for (int i = 1; i < i + 1; i++) {
            input[i] = sc.nextLine();
            if (input[i].charAt(1) == 'T')
                j.judgePaper(input[i]);
            if (input[i].charAt(1) == 'N')
                j.judgeQuestion(input[i]);
            l++;
            if (input[i].equals("end"))
                break;
            if ((input[i].charAt(1) != 'N' && input[i].charAt(1) != 'T' && input[i].charAt(1) != 'X' && input[i].charAt(1) != 'S' && input[i].charAt(1) != 'D')) {
                System.out.println("wrong format:" + input[i]);
            }
        }
        if (l == 2) {
            if (input[1].charAt(1) == 'S') {
                System.out.println("The test paper number does not exist");
                return;
            }
        }
        judge.setInput(input, l);
        String[] Question = judge.JudgeQuestion();
        String[] Paper = judge.JudgePaper();
        String[] AnswerPaper = judge.JudgeAnswerPaper();
        String[] StudentId = judge.JudgeStudentId();
        String[] Deleteinformation = judge.judgeDelete();
        Paper = judgeWrongFormat.judgePaper(Paper);
        Question = judgeWrongFormat.judgeQuestion(Question);
        for (int i = 1; i < Question.length; i++) {
            splitinput.setInput(Question[i]);
            splitinput.splitQuestion();
            if (splitinput.getQuestionNum() > QuestionSum) {
                QuestionSum = splitinput.getQuestionNum();
            } else {
                QuestionSum++;
            }
            question.setQuestion(QuestionSum, splitinput.getQuestionNum(), splitinput.getQuestion(), splitinput.getStandardAnswer());
        }
        for (int i = 1; i < Paper.length; i++) {
            splitinput.setInput(Paper[i]);
            splitinput.splitPaper();
            if (splitinput.getPaperNum() > PaperSum) {
                PaperSum = splitinput.getPaperNum();
            } else {
                PaperSum++;
            }
            scoresumjudge.setScore(PaperSum, splitinput.getPaperNum(), splitinput.getPaperQuestionNum(), splitinput.getPaperQuestionMark());
            answerquestion.setPaper(PaperSum, splitinput.getPaperNum());
            answerquestion.getPaper()[splitinput.getPaperNum()].setPaper(splitinput.getPaperQuestionSum(), splitinput.getPaperQuestionNum(), splitinput.getPaperQuestionMark(), question);
        }
        scoresumjudge.printResult();
        for (int i = 1; i < StudentId.length; i++) {
            splitinput.setInput(StudentId[i]);
            splitinput.splitStudentId();
            studentId.setStudentId(splitinput.getStudentId());
            studentId.setStudentName(splitinput.getStudentName());
        }
        for (int i = 1; i < Deleteinformation.length; i++) {
            splitinput.setInput(Deleteinformation[i]);
            splitinput.splitDeleteinformation();
            delete.setDeleteNum(splitinput.getDeleteNum());
        }
        for (int i = 1; i < AnswerPaper.length; i++) {
            splitinput.setInput(AnswerPaper[i]);
            splitinput.splitAnswer();
            if (splitinput.getAnswerPaperNum() > AnswerSum) {
                AnswerSum = splitinput.getAnswerPaperNum();
            } else {
                AnswerSum++;
            }
            answerquestion.setAnswerPaper(AnswerSum, splitinput.getAnswerPaperNum());
            answerquestion.getAnswerPaper()[splitinput.getAnswerPaperNum()].setAnswerPaper(splitinput.getAnswer(), splitinput.getAnswerNum(), splitinput.getAnswerStudentName(), splitinput.getAnswerPaperNum());
            if (answerquestion.getPaper().length == 1) {
                System.out.println("The test paper number does not exist");
                continue;
            }
            for (int k = 1; k <= answerquestion.getPaperSum(); k++) {//有问题
                if (answerquestion.getPaper()[k] != null && k == splitinput.getAnswerPaperNum()) {
                    agent.setAgent(answerquestion, question, delete, splitinput.getAnswerPaperNum(), studentId);
                    agent.JudgeAnswer();
                    break;
                }
                if (k == answerquestion.getPaperSum()) {
                    System.out.println("The test paper number does not exist");
                    break;
                }
            }
        }
    }
}

class JudgeWrongFormat2 {
    public void judgeQuestion(String input) {
        if (!(input.matches("^#N:(.*?) #Q:(.*?) #A:(.*)$"))) {
            System.out.println("wrong format:" + input);
        }
    }

    public void judgePaper(String input) {
        if (!(input.matches("^#T:( )*\\w+( )*(( )*\\w+( )*-( )*\\w+( )*)+"))) {
            System.out.println("wrong format:" + input);
        }
    }
}

class JudgeWrongFormat {
    private String input[];

    public JudgeWrongFormat() {

    }

    public JudgeWrongFormat(String input[]) {
        this.input = input;
    }

    public void setInput(String input[]) {
        this.input = input;
    }

    public String[] judgeQuestion(String input[]) {
        int b = 0;
        for (int i = 1; i < input.length; i++) {
            if (!(input[i].matches("^#N:\\s*(\\d+)\\s*#Q:\\s*(.*?)\\s*#A:\\s*(.*)"))) {
                input[i] = "#N:" + input[i].charAt(3) + "laji1";
                b++;
            }
        }
        return input;
    }

    public String[] judgePaper(String input[]) {
        int b = 0;
        for (int i = 1; i < input.length; i++) {
            if (!(input[i].matches("^#T:( )*\\w+( )*(( )*\\w+( )*-( )*\\w+( )*)+"))) {
                //System.out.println("wrong format:" + input[i]);
                b++;
            }
        }
        String[] input2 = new String[input.length - b];

        for (int i = 1, j = 1; i < input.length; i++) {
            if (input[i].matches("^#T:( )*\\w+( )*(( )*\\w+( )*-( )*\\w+( )*)+")) {
                input2[j] = input[i];
                j++;
            }
        }
        return input2;
    }

}

class SingleInformationInput {
    public boolean single(String input[], int l) {
        if (l + 1 == 3) {
            if (input[1].charAt(1) == 'S') {
                System.out.println("The test paper number does not exist");
            }
            if (input[1].charAt(1) == 'X' || input[1].charAt(1) == 'T' || input[1].charAt(1) == 'N' || input[1].charAt(1) == 'D') {
                System.out.println("");
            }
            return true;
        }
        return false;
    }
}

class Agent {
    private AnswerQuestion answerQuestion = new AnswerQuestion();
    private Question question = new Question();
    private Delete delete = new Delete();
    private StudentId studentId = new StudentId();
    private int AnswerNum;

    public void setAgent(AnswerQuestion answerQuestion, Question question, Delete delete, int answerNum, StudentId studentId) {
        this.answerQuestion = answerQuestion;
        this.delete = delete;
        AnswerNum = answerNum;
        this.question = question;
        this.studentId = studentId;
    }

    public Agent() {

    }

    public Agent(AnswerQuestion answerQuestion, Question question, Delete delete, int AnswerNum) {
        this.answerQuestion = answerQuestion;
        this.question = question;
        this.delete = delete;
        this.AnswerNum = AnswerNum;
    }

    public int[] printResult(int i, int mark, int a[], int ii) {
        if (answerQuestion.getPaper()[AnswerNum].getPaperQuestionNum()[answerQuestion.getAnswerPaper()[AnswerNum].getAnswerNum()[i]] > question.getStandardAnswer().length - 1) {
            System.out.println("non-existent question~0");
            a[ii] = 0;
        } else if (question.getStandardAnswer()[answerQuestion.getPaper()[AnswerNum].getPaperQuestionNum()[answerQuestion.getAnswerPaper()[AnswerNum].getAnswerNum()[i]]].equals("laji1")) {
            System.out.println("non-existent question~0");
            a[ii] = 0;
        } else if (question.getStandardAnswer()[answerQuestion.getPaper()[AnswerNum].getPaperQuestionNum()[answerQuestion.getAnswerPaper()[AnswerNum].getAnswerNum()[i]]].equals("laji")) {
            System.out.println("the question " + answerQuestion.getPaper()[AnswerNum].getPaperQuestionNum()[answerQuestion.getAnswerPaper()[AnswerNum].getAnswerNum()[i]] + " invalid~0");
            a[ii] = 0;
        } else {
            if (answerQuestion.getAnswerPaper()[AnswerNum].getAnswer()[i].equals(question.getStandardAnswer()[answerQuestion.getPaper()[AnswerNum].getPaperQuestionNum()[answerQuestion.getAnswerPaper()[AnswerNum].getAnswerNum()[i]]])) {
                System.out.println(question.getQuestion()[answerQuestion.getPaper()[AnswerNum].getPaperQuestionNum()[answerQuestion.getAnswerPaper()[AnswerNum].getAnswerNum()[i]]] + "~" + answerQuestion.getAnswerPaper()[AnswerNum].getAnswer()[i] + "~true");
                mark += answerQuestion.getPaper()[AnswerNum].getPaperQuestionMark()[answerQuestion.getAnswerPaper()[AnswerNum].getAnswerNum()[i]];
                a[ii] = answerQuestion.getPaper()[AnswerNum].getPaperQuestionMark()[answerQuestion.getAnswerPaper()[AnswerNum].getAnswerNum()[i]];
            } else {
                System.out.println(question.getQuestion()[answerQuestion.getPaper()[AnswerNum].getPaperQuestionNum()[answerQuestion.getAnswerPaper()[AnswerNum].getAnswerNum()[i]]] + "~" + answerQuestion.getAnswerPaper()[AnswerNum].getAnswer()[i] + "~false");
                a[ii] = 0;
            }
        }
        a[answerQuestion.getAnswerPaper()[AnswerNum].getAnswer().length + 199] += mark;
        return a;
    }

    public void JudgeAnswer() {
        int mark = 0;
        int c[] = new int[answerQuestion.getAnswerPaper()[AnswerNum].getAnswer().length + 200];
        int a[] = new int[answerQuestion.getAnswerPaper()[AnswerNum].getAnswer().length + 200];
        for (int i = 1; i < delete.getDeleteNum().length; i++) {
            if (delete.getDeleteNum()[i] < question.getStandardAnswer().length)
                question.getStandardAnswer()[delete.getDeleteNum()[i]] = "laji";
        }
        int b = answerQuestion.getPaper()[AnswerNum].getPaperQuestionNum().length;//answerQuestion.getAnswerPaper()[AnswerNum].getAnswer().length;
        answerQuestion.getAnswerPaper()[AnswerNum].bubble();
        for (int j = 1; j < answerQuestion.getPaper()[AnswerNum].getPaperQuestionNum().length; j++) {
            for (int i = 1; i < answerQuestion.getAnswerPaper()[AnswerNum].getAnswer().length; i++) {//暂时尝试一下,可能答卷的答案不按套路出牌
                if (answerQuestion.getAnswerPaper()[AnswerNum].getAnswerNum()[i] == j) {
                    a = this.printResult(i, mark, a, j);
                    break;
                }
                if (i == answerQuestion.getAnswerPaper()[AnswerNum].getAnswer().length - 1) {
                    a[j] = 0;
                    System.out.println("answer is null");
                }
            }
        }
        if (answerQuestion.getAnswerPaper()[AnswerNum].getAnswer().length == 1) {
            for (int i = 1; i < answerQuestion.getPaper()[AnswerNum].getPaperQuestionNum().length; i++) {
                System.out.println("answer is null");
            }
        }
        for (int i = 1; i < studentId.getStudentId().length; i++) {
            if (answerQuestion.getAnswerPaper()[AnswerNum].getAnswerStudent().equals(studentId.getStudentId()[i])) {
                System.out.printf("%s %s: ", studentId.getStudentId()[i], studentId.getStudentName()[i]);
                break;
            }
            if (i == studentId.getStudentId().length - 1) {
                System.out.println(answerQuestion.getAnswerPaper()[AnswerNum].getAnswerStudent() + " not found");
                return;
            }
        }
        for (int i = 1; i < b; i++) {
            if (i != b - 1)
                System.out.printf("%d ", a[i]);
            else
                System.out.printf("%d", a[i]);
        }
        System.out.println("~" + a[answerQuestion.getAnswerPaper()[AnswerNum].getAnswer().length + 199]);
    }
}

class Judge {
    private String[] input;
    private int length;
    private int k = 1;
    private int m = 1;

    public Judge() {

    }

    public Judge(String[] input, int length) {
        this.input = new String[length];
        for (int i = 1; i < length; i++) {
            this.input[i] = input[i];
        }
    }

    public void setInput(String[] input, int length) {
        this.input = new String[length];
        for (int i = 1; i < length; i++) {
            this.input[i] = input[i];
        }
    }

    public String[] JudgeQuestion() {
        for (int i = 1; i <= this.input.length - 1; i++) {
            if (input[i].charAt(1) == 'N') {
                k++;
            }
        }
        String[] Question = new String[k];
        for (int i = 1; i <= this.input.length - 1; i++) {
            if (input[i].charAt(1) == 'N') {
                Question[m] = input[i];
                m++;
            }
        }
        m = 1;
        k = 1;
        return Question;
    }

    public String[] JudgePaper() {
        for (int i = 1; i <= this.input.length - 1; i++) {
            if (input[i].charAt(1) == 'T') {
                k++;
            }
        }
        String[] Paper = new String[k];
        for (int i = 1; i <= this.input.length - 1; i++) {
            if (input[i].charAt(1) == 'T') {
                Paper[m] = input[i];
                m++;
            }
        }
        m = 1;
        k = 1;
        return Paper;
    }

    public String[] JudgeAnswerPaper() {
        for (int i = 1; i <= this.input.length - 1; i++) {
            if (input[i].charAt(1) == 'S') {
                k++;
            }
        }
        String[] AnswerPaper = new String[k];
        for (int i = 1; i <= this.input.length - 1; i++) {
            if (input[i].charAt(1) == 'S') {
                AnswerPaper[m] = input[i];
                m++;
            }
        }
        k = 1;
        m = 1;
        return AnswerPaper;
    }

    public String[] JudgeStudentId() {
        String StudentId[] = new String[2];
        for (int i = 1; i <= this.input.length - 1; i++) {
            if (input[i].charAt(1) == 'X') {
                StudentId[1] = input[i];
                break;
            }
        }
        k = 1;
        m = 1;
        return StudentId;
    }

    public String[] judgeDelete() {
        for (int i = 1; i <= this.input.length - 1; i++) {
            if (input[i].charAt(1) == 'D') {
                k++;
            }
        }
        String[] Deleteinformation = new String[k];
        for (int i = 1; i <= this.input.length - 1; i++) {
            if (input[i].charAt(1) == 'D') {
                Deleteinformation[m] = input[i];
                m++;
            }
        }
        m = 1;
        k = 1;
        return Deleteinformation;
    }
}

class SplitInput {
    private String Question;
    private String QuestionNum;
    private String StandardAnswer;
    private String Questionmark;
    private String input;
    private int PaperQuestionSum;
    private int AnswerPaperNum;
    private String[] Answer;//= new String[AnswerpaperNum+1];
    private int[] AnswerNum;
    private String PaperNum;
    private int[] PaperQuestionNum;
    private int[] PaperQuestionMark;
    private String[] StudentId;
    private String[] StudentName;
    private int DeleteNum;
    private String AnswerStudentName;

    public SplitInput() {

    }

    public SplitInput(String input) {
        this.input = input;
    }

    public void setInput(String input) {
        this.input = input;
    }

    public void splitQuestion() {
        if (input.contains("laji1")) {
            this.QuestionNum = String.valueOf(input.charAt(3));
            this.Question = "laji1";
            this.StandardAnswer = "laji1";
            return;
        }
        input = input.replaceAll(" #A", "");
        String s[] = input.split(":");
        while (s[1].charAt(s[1].length() - 1) == ' ' || s[1].charAt(s[1].length() - 1) == '#' || s[1].charAt(s[1].length() - 1) == 'Q') {
            s[1] = s[1].substring(0, s[1].length() - 1);
        }
        while (s[1].charAt(0) == ' ') {
            s[1] = s[1].substring(1, s[1].length());
        }
        this.QuestionNum = s[1];
        this.Question = s[2];
        if (s.length == 4) {
            if (s[3].matches("\\s+")) {
                this.StandardAnswer = "";
                return;
            }
            while (s[3].charAt(s[3].length() - 1) == ' ') {
                s[3] = s[3].substring(0, s[3].length() - 1);
            }
            while (s[3].charAt(0) == ' ') {
                s[3] = s[3].substring(1, s[3].length());
            }
            this.StandardAnswer = s[3];
        } else {
            this.StandardAnswer = "";
        }
    }

    public void splitPaper() {
        input = input.replaceAll("#T:", "");
        while (input.charAt(0) == ' ') {
            input = input.substring(1, input.length());
        }
        String[] s = input.split("\\s+");
        PaperQuestionNum = new int[s.length];
        PaperQuestionMark = new int[s.length];
        PaperNum = s[0];
        PaperQuestionSum = s.length - 1;
        for (int i = 1; i < s.length; i++) {
            String[] ss = s[i].split("-");
            PaperQuestionNum[i] = Integer.parseInt(ss[0]);
            PaperQuestionMark[i] = Integer.parseInt(ss[1]);
        }
    }

    public void splitAnswer() {
        String p = input;
        p = p.replaceAll("#S:", "");
        p = p.replaceAll("#A:", "");
        String[] s1 = p.split("\\s+");
        this.AnswerStudentName = s1[1];
        this.AnswerPaperNum = Integer.parseInt(s1[0]);
        String s = input.replaceAll("^#S:\\d+ \\d{8} ", "");
        input = input.substring(1, input.length());
        s = s.replaceAll("#A", "");
        String S[] = s.split(":");
        this.Answer = new String[S.length];
        this.AnswerNum = new int[S.length];
        for (int i = 1; i < S.length; i++) {
            String ss[] = S[i].split("-");
            this.AnswerNum[i] = Integer.parseInt(ss[0]);
            if (ss.length == 2) {
                if (ss[1].matches("\\s+") && ss[1] != " ") {
                    this.Answer[i] = "";
                    continue;
                }
                if (i != S.length - 1) {
                    if (ss[1].length() == 1 && ss[1].charAt(ss[1].length() - 1) == ' ') {
                        this.Answer[i] = "";
                        continue;
                    }
                }
                while (ss[1].charAt(ss[1].length() - 1) == ' ') {
                    ss[1] = ss[1].substring(0, ss[1].length() - 1);
                }
                while (ss[1].charAt(0) == ' ') {
                    ss[1] = ss[1].substring(1, ss[1].length());
                }
                this.Answer[i] = ss[1];
            } else {
                this.Answer[i] = "";
            }
        }
    }

    public void splitStudentId() {
        input = input.replaceAll("#X:", "");
        String[] s = input.split("-");
        StudentId = new String[s.length + 1];
        StudentName = new String[s.length + 1];
        for (int i = 0; i < s.length; i++) {
            String[] ss = s[i].split("\\s+");
            StudentId[i + 1] = ss[0];
            StudentName[i + 1] = ss[1];
        }
    }

    public void splitDeleteinformation() {
        input = input.replaceAll("#D:N-", "");
        DeleteNum = Integer.parseInt(input);
    }

    public int getQuestionNum() {
        return Integer.parseInt(this.QuestionNum);
    }

    public String getQuestion() {
        return this.Question;
    }

    public String getStandardAnswer() {
        return this.StandardAnswer;
    }

    public int getPaperQuestionSum() {
        return PaperQuestionSum;
    }

    public int getPaperNum() {
        return Integer.parseInt(this.PaperNum);
    }

    public int[] getPaperQuestionNum() {
        return this.PaperQuestionNum;
    }

    public int[] getPaperQuestionMark() {
        return this.PaperQuestionMark;
    }

    public int getAnswerPaperNum() {
        return this.AnswerPaperNum;
    }

    public String getAnswerStudentName() {
        return this.AnswerStudentName;
    }

    public String[] getAnswer() {
        return this.Answer;
    }

    public int[] getAnswerNum() {
        return this.AnswerNum;
    }

    public String[] getStudentId() {
        return this.StudentId;
    }

    public String[] getStudentName() {
        return this.StudentName;
    }

    public int getDeleteNum() {
        return this.DeleteNum;
    }
}

class ScoreSumJudge {
    private int MarkSum;
    private int PaperSum = 0;
    private String result[] = new String[PaperSum + 1];
    private int PaperQuestionNum[] = new int[PaperSum + 1];
    private int score[] = new int[PaperSum + 1];

    public ScoreSumJudge() {

    }

    public void upDate() {
        String result2[] = new String[PaperSum + 1];
        for (int i = 1; i < this.result.length; i++) {
            result2[i] = result[i];
        }
        result = new String[PaperSum];
        result = result2;
    }

    public void setScore(int PaperSum, int PaperNum, int[] PaperQuestionNum, int[] PaperQuestionMark) {
        this.PaperSum = PaperSum;
        this.MarkSum = 0;
        this.upDate();
        for (int i = 1; i < PaperQuestionNum.length; i++) {
            MarkSum += PaperQuestionMark[i];
        }
        if (MarkSum != 100)
            result[PaperNum] = "alert: full score of test paper" + PaperNum + " is not 100 points";
        else
            result[PaperNum] = "";
    }

    public void printResult() {
        for (int i = 1; i < result.length; i++) {
            if (result[i] != null && !(result[i].equals(""))) {
                System.out.println(result[i]);
            }
        }
    }
}


class AnswerQuestion {
    private int AnswerSum = 0;
    private int PaperSum = 0;
    private AnswerPaper answerPaper[] = new AnswerPaper[AnswerSum + 1];
    private Paper paper[] = new Paper[PaperSum + 1];

    public AnswerQuestion() {

    }

    public int getPaperSum() {
        return this.PaperSum;
    }

    public int getAnswerSum() {
        return this.PaperSum;
    }

    public void upDatePaper() {
        Paper paper2[] = new Paper[PaperSum + 1];
        for (int i = 1; i < paper.length; i++) {
            paper2[i] = paper[i];
        }
        this.paper = new Paper[PaperSum + 1];
        this.paper = paper2;
    }

    public void upDateAnswerPaper() {
        AnswerPaper answerPaper2[] = new AnswerPaper[AnswerSum + 1];
        for (int i = 1; i < answerPaper.length; i++) {
            answerPaper2[i] = answerPaper[i];
        }
        answerPaper = new AnswerPaper[AnswerSum + 1];
        answerPaper = answerPaper2;
    }

    public void setPaper(int PaperSum, int PaperNum) {
        this.PaperSum = PaperSum;
        this.upDatePaper();
        paper[PaperNum] = new Paper();
    }

    public Paper[] getPaper() {
        return this.paper;
    }

    public AnswerPaper[] getAnswerPaper() {
        return this.answerPaper;
    }

    public void setAnswerPaper(int AnswerSum, int AnswerNum) {
        this.AnswerSum = AnswerSum;
        this.upDateAnswerPaper();
        answerPaper[AnswerNum] = new AnswerPaper();
    }
}

class AnswerPaper {
    private int AnswerPaperNum;
    private String[] Answer;
    private int[] AnswerNum;
    private String AnswerStudent;

    public AnswerPaper() {

    }

    public AnswerPaper(int AnswerPaperNum, String[] Answer, int[] AnswerNum) {
        this.AnswerNum = AnswerNum;
        this.AnswerPaperNum = AnswerPaperNum;
        this.Answer = Answer;
    }

    public void bubble() {
        for (int i = 1; i < AnswerNum.length - 1; i++) {
            for (int j = 1; j < AnswerNum.length - 1; j++) {
                if (AnswerNum[j] > AnswerNum[j + 1]) {
                    String a = Answer[j];
                    int b = AnswerNum[j];
                    Answer[j] = Answer[j + 1];
                    AnswerNum[j] = AnswerNum[j + 1];
                    Answer[j + 1] = a;
                    AnswerNum[j + 1] = b;
                }
            }
        }
    }

    public int getAnswerPaperNum() {
        return AnswerPaperNum;
    }

    public int[] getAnswerNum() {
        return AnswerNum;
    }

    public String getAnswerStudent() {
        return AnswerStudent;
    }

    public void setAnswerPaper(String[] Answer, int[] AnswerNum, String AnswerStudent, int AnswerPaperNum) {
        this.Answer = Answer;
        this.AnswerNum = AnswerNum;
        this.AnswerStudent = AnswerStudent;
        this.AnswerPaperNum = AnswerPaperNum;
    }


    public String[] getAnswer() {
        return this.Answer;
    }
}

class Paper {
    private int PaperSum = 0;  //试卷题目数量;
    private int PaperQuestionNum[] = new int[PaperSum + 1];//题目标号
    private int PaperQuestionMark[] = new int[PaperSum + 1];//编号下对应分数
    private Question question = new Question();

    public Paper() {

    }

    public int[] getPaperQuestionNum() {
        return this.PaperQuestionNum;
    }

    public int[] getPaperQuestionMark() {
        return this.PaperQuestionMark;
    }

    public int getPaperSum() {
        return this.PaperSum;
    }

    public Paper(int PaperSum, int[] PaperQuestionNum, int PaperQuestionMark[], Question question) {
        this.PaperSum = PaperSum;
        this.PaperQuestionNum = new int[PaperSum + 1];
        this.PaperQuestionMark = new int[PaperSum + 1];
        this.PaperQuestionNum = PaperQuestionNum;
        this.PaperQuestionMark = PaperQuestionMark;
    }

    public void setPaper(int PaperSum, int[] PaperQuestionNum, int PaperQuestionMark[], Question question) {
        this.PaperSum = PaperSum;
        this.PaperQuestionNum = new int[PaperSum + 1];
        this.PaperQuestionMark = new int[PaperSum + 1];
        this.PaperQuestionNum = PaperQuestionNum;
        this.PaperQuestionMark = PaperQuestionMark;
    }
}

class Question {
    private int QuestionSum = 0;
    private String Question[] = new String[QuestionSum + 1];
    private String StandardAnswer[] = new String[QuestionSum + 1];

    public Question() {
    }

    public void upDate() {
        String Question2[] = new String[this.QuestionSum + 1];
        String StandardAnswer2[] = new String[this.QuestionSum + 1];
        for (int i = 1; i < Question2.length; i++) {
            Question2[i] = "laji1";
            StandardAnswer2[i] = "laji1";
        }
        for (int i = 1; i < Question.length; i++) {
            Question2[i] = Question[i];
            StandardAnswer2[i] = StandardAnswer[i];
        }
        this.Question = new String[this.QuestionSum + 1];
        this.StandardAnswer = new String[this.QuestionSum + 1];
        Question = Question2;
        StandardAnswer = StandardAnswer2;
    }

    public void setQuestion(int QuestionSum, int QuestionNum, String Question, String StandardAnswer) {
        this.QuestionSum = QuestionSum;
        this.upDate();
        this.Question[QuestionNum] = Question;
        this.StandardAnswer[QuestionNum] = StandardAnswer;
    }

    public String[] getQuestion() {
        return this.Question;
    }

    public String[] getStandardAnswer() {
        return this.StandardAnswer;
    }
}

class Delete {
    private int num = 1;
    private int DeleteNum[] = new int[num];

    public Delete() {

    }

    public int getNum() {
        return num;
    }

    public int[] getDeleteNum() {
        return DeleteNum;
    }

    public void upDateDeleteNum() {
        int DeleteNum2[] = new int[num];
        for (int i = 1; i < this.DeleteNum.length; i++) {
            DeleteNum2[i] = DeleteNum[i];
        }
        DeleteNum = new int[num];
        DeleteNum = DeleteNum2;
    }

    public void setDeleteNum(int DeleteNum) {
        num++;
        upDateDeleteNum();
        this.DeleteNum[this.DeleteNum.length - 1] = DeleteNum;
    }
}

class StudentId {
    private String StudentId[];
    private String StudentName[];

    public StudentId(String[] studentId, String[] studentName) {
        StudentId = studentId;
        StudentName = studentName;
    }

    public String[] getStudentId() {
        return StudentId;
    }

    public void setStudentId(String[] studentId) {
        StudentId = studentId;
    }

    public String[] getStudentName() {
        return StudentName;
    }

    public void setStudentName(String[] studentName) {
        StudentName = studentName;
    }

    public StudentId() {
    }
}

 

 第三次答题程序又在第二题基础上又增加了学生信息,删除题目,错误格式的判断,还有空白试卷等特殊情况;

相较于上一次在splitinput类中增加了对学生信息和删除信息的分离,由于第二次的代码可复用,思路也是沿用上一次的思路。这次增加了很多类,特别的增加了一个agent类,用来处理数据和输出。

但是写着写着我发现了一个问题主方法里面的对象太多了,这是一种错误的行为(代码可读性和维护性会很差)。

不过这次的测试点是真的难过(感觉有一两个测试点涵盖的内容有很多都没说,要去猜)

踩坑心得:

第一次pta测试时没有什么很纠结的测试点。

而第二次答题程序其实注意一下题目和答案就可以了

测试样例

#N:3 #Q:idaf fdaf daf #A:5 fda dfa
#N:2 #Q:2+2= #A:4
#T:1 3-7 2-6
#S:1 #A:5 #A:22
#N:1 #Q:1+1= #A:2 da dsa
#T:2 2-5 1-3 3-2
#S:2 #A:5 #A:4
end

结果

alert: full score of test paper1 is not 100 points
alert: full score of test paper2 is not 100 points
idaf fdaf daf~5~false
2+2=~22~false
0 0~0
2+2=~5~false
1+1=~4~false
answer is null
0 0 0~0

就比如说这个题目内有空格和答案有空格不能用空格分割。。。

最无语的其实就是第三次的测试点 上素材:

输入

#T:1 1-8 1-9 1-9 6-9 3-6 1-65
#X:20201103 Tom-20201104 Jack-20201105 Www
#S:1 20201103 #A:3-    |      #A:6- 
#N:1 #Q:1+1= #A: 
end

结果

alert: full score of test paper1 is not 100 points
answer is null
answer is null
1+1=~|~false
answer is null
answer is null
1+1=~~true
20201103 Tom: 0 0 0 0 0 65~65

输入

#N:1 #Q:1+1= #A: 4
#T:1 1-5 7-8 8-9 9-2 2-8 10-6
#X:20201103 Tomgrrrrrrrrrrrrrrrrr-20201104 Jack-20201104 Www
#S:1 20201103 #A:2-9 #A:9- 9 #A:1- 4
#S:2 20201103
#D:N-2
#T:2 1-4
#N:2 #Q:2+2= #A:4
end

结果

alert: full score of test paper1 is not 100 points
alert: full score of test paper2 is not 100 points
1+1=~4~true
non-existent question~0
answer is null
answer is null
answer is null
answer is null
20201103 Tomgrrrrrrrrrrrrrrrrr: 5 0 0 0 0 0~5
answer is null
20201103 Tomgrrrrrrrrrrrrrrrrr: 0~0

输入

#N:1 #Q:1+1= #A:    
#N:2 #Q:2+2= #A:     caonima      
#N:3 #Q:6+6= #A:                   
#T:1 3-88 2-8 1-4 6-9 5-7 9-9 10-99
#D:N-3
#X:11111111 Tom-20201104 Jack-20201105 Www
#N:5 #Q:2+2= #A:      282     
#S:1 11111111 #A:2-      caonima                 #A:3- #A:1- #A:5-282 #A:4-99
#N:9 #Q:2+2= #A:         828  
end

输出

alert: full score of test paper1 is not 100 points
the question 3 invalid~0
2+2=~caonima~true
1+1=~~true
non-existent question~0
2+2=~282~true
answer is null
answer is null
11111111 Tom: 0 8 4 0 7 0 0~19

这些测试点必须得都过才能全对,这种标准答案可以有空格,回答的答案必须去掉,一开始写代码时是真的没有去注意,但是后面的测试点有要满足这种情况才可以过,所以当时写这一题真的花了好久的时间

还有就是:

输入:

dgn#Q:1+1= #A:2
#N:2 #Q:2+2= #A:4
#T:1 1-5 2-8
#X:20201103 Tom-20201104 Jack-20201105 Www
#S:1 20201103 #A:1-5 #A:2-4
#D:N-2
end

输出:

wrong format:dgn#Q:1+1= #A:2
alert: full score of test paper1 is not 100 points
non-existent question~0
the question 2 invalid~0
20201103 Tom: 0 0~0

之前只考虑了对错误格式的内部出错(就是前面的#Q这些保留,后面的格式错误)结果有几个测试点就是过不了,太难受了,所以一定要对多种情况进行考虑

在进行代码书写时某些细致问题没有注意导致出错,所以需要仔细认真

 

改进建议:

对于main函数方法中的对象创建太多的问题必须要进行改进,提高代码的可读性。

类要保证单一职责的原则,书写类时要遵守这一点

某一个类不能写的太多,不然类的维护性会非常差

 

总结:

题目在不断变化,而我们的代码的类的设计需要有可扩展性,对于类的书写要满足类的特性。叹,写完这次博客,pta又一次的迭代题目又要来了,加油。

 

 

 

 

 

 

 
 
 

 

 

 

热门相关:国民女神:重生王牌千金   重生之大文豪   绝宠小医妃:王爷,来一针   江太太恃宠而骄   逼婚首席:影后前妻很抢手