首页 > 其他分享 >NCHU题目集1~3的总结

NCHU题目集1~3的总结

时间:2024-04-21 19:36:09浏览次数:32  
标签:总结 题目 String get NCHU matcher int key public

目录

一 . 前言
二 . 设计与分析
三 . 采坑心得
四 . 改进建议
五 . 总结

前言

知识点总结

题目集一

  1. 字符串处理
    对输入的字符串进行对应的匹配与确认,找到对应的字符串的内容,将对应字符串中不合规范的内容进行处理,合格后直接存储。
    字符串的比对,满足要求的字符串进行输出和相应的返回。

  2. 字符串数组
    创建满足要求的字符串数组,将字符串进行赋值。
    找到对应的字符串,通过下标法将其中内容进行获取。
    通过对应下标将对应内容进行需要的操作。

题目集二

  1. 正则表达式
    创建合适的正则表达式。
    通过正则表达式将字符串进行快速的处理。
    将匹配到的字符串进行获取和存储。

  2. 字符串处理
    字符串比对和所需字符串格式的处理。

  3. 动态数组
    创建动态数组,以及对应内容的存储。
    通过ArrayList.add()方法增加内容。
    使用ArrayList.get()方法获取。

题目集三

  1. 哈希表
    创建哈希表,以合适方式进行数据存储。
    通过key进行对应数据的获取。
    用其中的方法进行key的确认和匹配。

  2. 正则表达式
    创建合适的正则表达式。
    将字符串匹配后获取。

  3. 字符串处理

题量

9小题:
设计一个风扇Fan类
类和对象的使用
成绩计算-1-类、数组的基本运用
成绩计算-2-关联类
手机按价格排序、查找
求圆的面积(类与对象)
Java类与对象-汽车类
面向对象编程(封装性)
日期类的基本使用

3大题:难度逐渐增加,复杂度同时逐渐提升。

难度

9小题难度不大,3大题难度递增。
第一次作业:
第一次作业难度不大,主要包括了字符串的使用和数组的使用,如果会正则表达式的话,这题目会更没有难度。因为数组大小固定且没有过多的数据类型和对应方法,所以在复杂程度上并不算难,并且如果不会正则表达式可以直接使用字符串的处理方法。对第一次作业可以解决的方案很多。

第二次作业:
第二次作业难度提升,增加了试卷和答卷,试卷题目来源于题目库,且有错误引用。判题要求有所增加,不仅要进行答案匹配还要题目顺序得分的匹配。试题数量和答案数量的不确定提供了更多的操作空间。因此难度上升了。

第三次作业:
第三次作业难度提升主要体现在对于不同类型的数据存储和对应的处理。输入字符串中有多种不同的错误,对于不同的错误有多种不同的反馈。这使得许多输入需要多种匹配方式。
此外还有对于不同类之间的组合应用。许多不同类型数据的比对,使得需要不同的数据有多样的比对。

设计与分析

第一次作业

  1. 题目类
    包含信息:题号(int),题目(String),答案(String)。在通过进行格式:"#N:"+题号+" "+"#Q:"+题目内容+" "#A:"+标准答案,的字符串匹配后,找到对应内容后以题号作为题目在题目数组中的顺序号进行存储。
    其作用为封装存储每个题目的信息。
class Subject {
    private int num;
    private String problem;
    private String answer;
    public Subject(int num, String problem, String answer) {
        this.num = num;
        this.problem = problem;
        this.answer = answer;
    }
    public String getanswer(){
        return answer;
    }
    public String getproblem(){
        return problem;
    }
    public int getnum(){
        return num;
    }
}
  1. 试卷类
    建立题目数组,将每个题目按题号顺序存储进入数组。
    其作用为建立题目类数组,存储题目信息。
class Exam {
    private int n;
    public Subject[] arr = new Subject[n];

    public Exam(Subject[] arr) {
        this.arr = arr;
    }
}
  1. 答卷类
    包含信息:判题结果(boolean),我的答案(String),确认答案对错的方法。在进行完答卷内容的存储后,通过字符串的比对来确认答案的对错(因为输入的答案与题目题号是其在对应数组的顺序,所以可以直接通过顺序号来实现对应题目的查找)。
    其作用为存储答卷信息。
class Paper {
    private String ianswer;
    private boolean fin;
    public Paper(String ianswer){
        this.ianswer = ianswer;
    }
    public String getianswer(){
        return ianswer;
    }
    public void check(String a,String b){
        if(a.equals(b)){
            this.fin = true;
            System.out.printf("true");
        }
        else{
            this.fin = false;
            System.out.printf("false");
        }
    }
}
  1. 主函数
    包含字符串输入,字符串比对,处理字符串为正常格式。
public class Main {
    public static void main(String[] agrs) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        String huancun = input.nextLine();
        Subject[] arr = new Subject[n];
        for (int i = 0; i < n; i++) {
            String line = input.nextLine();
            int nf = line.length();
            int n1 = line.indexOf("#N:");
            int n2 = line.indexOf("#Q:");
            int n3 = line.indexOf("#A:");
            int cnt1 = 3;
            int cnt2 = 3;
            int cnt3 = 3;
            while(line.charAt(n1+cnt1)==' '){
                cnt1++;
            }
            while(line.charAt(n2+cnt2)==' '){
                cnt2++;
            }
            while(line.charAt(n3+cnt3)==' '){
                cnt3++;
            }
            String str1 = line.substring(n1+cnt1,n2-1);
            String str2 = line.substring(n2+cnt2,n3-1);
            String str3 = line.substring(n3+cnt3,nf);
            int num = Integer.parseInt(str1);
            arr[num-1] = new Subject(num, str2, str3);
        }
        Exam exam = new Exam(arr);
        Paper[] me = new Paper[n];
        for(int i = 0; i < n; i ++){
            String str = input.next();
            int nl = str.length();
            int n5 = str.indexOf("#A:");
            String str5 = str.substring(n5+3,nl);
            me[i] = new Paper(str5);
        }
        String end = input.next();
        input.close();
        for(int i =0 ;i < n; i ++){
            System.out.printf("%s~%s\n",arr[i].getproblem(),me[i].getianswer());
        }
        for( int i = 0; i < n; i ++){
            me[i].check(me[i].getianswer(), exam.arr[i].getanswer());
            if(i!=n-1){
                System.out.printf(" ");
            }
        }
    }
}
  1. 完整代码

将输入的数据先进行存储,答卷类中创建一个题目类创建数组,题目信息存储在题目类数组之中。类中的信息用private进行封装,使其只能通过get和set函数进行数据的处理。

同时属于答卷的信息识别后存储于答卷类数组中。因为输入格式可能有错误,所以在存入数据前进行了数据格式规范化的处理。采用的处理方法为字符串的比对和处理多与空

格。最后在输入完成后进行答案的比对来确认对错。比对答案这里采用的也是字符串的比对方法。通过写这段代码,我对如何设计类、封装属性和方法有了更深入的了解。同

时,熟悉了使用数组来存储和处理多个对象的信息。另外,掌握了通过Scanner类来获取用户输入和使用String类进行格式化输出的方法。

完整函数
import java.util.Scanner;

class Subject {
    private int num;
    private String problem;
    private String answer;

    public Subject(int num, String problem, String answer) {
        this.num = num;
        this.problem = problem;
        this.answer = answer;
    }
    public String getanswer(){
        return answer;
    }
    public String getproblem(){
        return problem;
    }
    public int getnum(){
        return num;
    }
}

class Exam {
    private int n;
    public Subject[] arr = new Subject[n];

    public Exam(Subject[] arr) {
        this.arr = arr;
    }
}

class Paper {
    private String ianswer;
    private boolean fin;
    public Paper(String ianswer){
        this.ianswer = ianswer;
    }
    public String getianswer(){
        return ianswer;
    }
    public void check(String a,String b){
        if(a.equals(b)){
            this.fin = true;
            System.out.printf("true");
        }
        else{
            this.fin = false;
            System.out.printf("false");
        }
    }
}

public class Main {
    public static void main(String[] agrs) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        String huancun = input.nextLine();
        Subject[] arr = new Subject[n];
        for (int i = 0; i < n; i++) {
            String line = input.nextLine();
            int nf = line.length();
            int n1 = line.indexOf("#N:");
            int n2 = line.indexOf("#Q:");
            int n3 = line.indexOf("#A:");
            int cnt1 = 3;
            int cnt2 = 3;
            int cnt3 = 3;
            while(line.charAt(n1+cnt1)==' '){
                cnt1++;
            }
            while(line.charAt(n2+cnt2)==' '){
                cnt2++;
            }
            while(line.charAt(n3+cnt3)==' '){
                cnt3++;
            }
            String str1 = line.substring(n1+cnt1,n2-1);
            String str2 = line.substring(n2+cnt2,n3-1);
            String str3 = line.substring(n3+cnt3,nf);
            int num = Integer.parseInt(str1);
            arr[num-1] = new Subject(num, str2, str3);
        }
        Exam exam = new Exam(arr);
        Paper[] me = new Paper[n];
        for(int i = 0; i < n; i ++){
            String str = input.next();
            int nl = str.length();
            int n5 = str.indexOf("#A:");
            String str5 = str.substring(n5+3,nl);
            me[i] = new Paper(str5);
        }
        String end = input.next();
        input.close();
        for(int i =0 ;i < n; i ++){
            System.out.printf("%s~%s\n",arr[i].getproblem(),me[i].getianswer());
        }
        for( int i = 0; i < n; i ++){
            me[i].check(me[i].getianswer(), exam.arr[i].getanswer());
            if(i!=n-1){
                System.out.printf(" ");
            }
        }
    }
}

第二次作业

  1. 题目类
    包含信息:题号(String),题目(String),答案(String)。用于存储题目信息。
class Project{
    private String num;
    private String question;
    private String answer;
    Project (String num,String question,String answer){
        this.num = num;
        this.question = question;
        this.answer = answer;
    }
    public String getNum(){
        return num;
    }
    public String getQuestion(){
        return question;
    }
    public String getAnswer(){
        return answer;
    }
    public void setNum(String num){
        this.num = num;
    }
    public void setQuestion(String question){
        this.question = question;
    }
    public void setAnswer(String answer){
        this.answer = answer;
    }
}
  1. 试题类
    包含信息:题号(String),题目(String),答案(String),对应分值(int),所得分值(int)。用于存储试题信息。
class Exam{
    private String examanswer;
    private String examnum;
    private String examquestion;
    private int score;
    private int eachscore = 0;
    Exam(String examnum, int score){
        this.examnum = examnum;
        this.score = score;
    }
    public String getExamQuestion(){
        return examquestion;
    }
    public String getExamAnswer(){
        return examanswer;
    }
    public String getExamNum(){
        return examnum;
    }
    public int getScore(){
        return score;
    }
    public int getEachScore(){
        return eachscore;
    }
    public void setExamAnswer(String examanswer){
        this.examanswer = examanswer;
    }
    public void setExamQuestion(String examquestion){
        this.examquestion = examquestion;
    }
    public void setExamNum(String examnum){
        this.examnum = examnum;
    }
    public void setScore(int score){
        this.score = score;
    }
    public void setEachScore(int eachscore){
        this.eachscore = eachscore;
    }
}
  1. 试卷类
    包含信息:答卷名(String),卷面总分(int),所得总分(int),试题动态数组(ArrayList)。用于存储试卷信息。
class ExamBox{
    private String name;
    private int allScore = 0;
    private int myscore = 0;
    ArrayList<Exam> list = new ArrayList<Exam>();
    ExamBox(String name,int allScore){
        this.name = name;
        this.allScore = allScore;
    }
    public String getName(){
        return name;
    }
    public int getAllScore(){
        return allScore;
    }
    public int getMyScore(){
        return myscore;
    }
    public void setName(String name){
        this.name = name;
    }
    public void setAllScore(int allScore){
        this.allScore = allScore;
    }
    public void setMyScore(int myscore){
        this.myscore = myscore;
    }
    public String toCheck(){
        return "alert: full score of test paper"+getName()+" is not 100 points";
    }
}
  1. 答题类
    包含信息:我的答案(String)。用于存储答题信息。
class Paper{
    private String myanswer;
    Paper(String myanswer,int eachscore){
        this.myanswer = myanswer;
    }
    public String getMyAnswer(){
        return myanswer;
    }
    public void setMyAnswer(String myanswer){
        this.myanswer = myanswer;
    }
}
  1. 答卷类
    包含信息:答卷名(String),答题动态数组(ArrayList)。用于存储答卷信息。
class PaperBox{
    private String name;
    ArrayList<Paper> list = new ArrayList<Paper>();
    PaperBox(String name,int myscore){
        this.name = name;
    }
    public String getName(){
        return name;
    }
    public void setName(String name){
        this.name = name;
    }
}
  1. 主函数
    包含信息:题目动态数组(ArrayList),试卷动态数组(ArrayList),答卷动态数组(ArrayList)。
public class Main{
    public static void main(String[] args) {
        ArrayList<Project> list = new ArrayList<Project>();
        ArrayList<ExamBox> examlist = new ArrayList<ExamBox>();
        ArrayList<PaperBox> paperlist = new ArrayList<PaperBox>();
        Scanner input = new Scanner(System.in);
        String content = "null";
        while(!content.equals("end")){
            content = input.nextLine();
            if(content.charAt(1) == 'N'){
                Pattern pattern = Pattern.compile("#N:(\\w+) #Q:(.+) #A:(.+)");
                Matcher matcher = pattern.matcher(content);
                Project project = null;
                while(matcher.find()){
                    project = new Project(matcher.group(1),matcher.group(2),matcher.group(3));
                }
                list.add(project);
            }
            else if (content.charAt(1) == 'T'){
                Pattern pattern = Pattern.compile("#T:(\\w+)");
                Matcher matcher = pattern.matcher(content);
                ExamBox exambox = null;
                while(matcher.find()){
                    exambox = new ExamBox(matcher.group(1), 0);
                }
                pattern = Pattern.compile("([\\u4E00-\\u9FA5A-Za-z0-9_]+)-([\\u4E00-\\u9FA5A-Za-z0-9_]+)");
                matcher = pattern.matcher(content);
                int sum = 0;
                while(matcher.find()){
                    sum += Integer.parseInt(matcher.group(2));
                    // System.out.println(Integer.parseInt(matcher.group(2)));
                    Exam exam = new Exam(matcher.group(1), Integer.parseInt(matcher.group(2)));
                    exambox.list.add(exam);
                }
                exambox.setAllScore(sum);
                for(int i = 0; i < exambox.list.size(); i ++){
                    for(int j = 0; j < list.size(); j ++){
                        if(exambox.list.get(i).getExamNum().equals(list.get(j).getNum())){
                            exambox.list.get(i).setExamAnswer(list.get(j).getAnswer());
                            exambox.list.get(i).setExamQuestion(list.get(j).getQuestion());
                        }
                    }
                }
                examlist.add(exambox);
            }
            else if (content.charAt(1) == 'S'){
                Pattern pattern = Pattern.compile("#S:(\\w+)");
                Matcher matcher = pattern.matcher(content);
                PaperBox paperbox = null;
                while(matcher.find()){
                    paperbox = new PaperBox(matcher.group(1), 0);
                }
                pattern = Pattern.compile("#A:([\\u4E00-\\u9FA5A-Za-z0-9_]+)");
                matcher = pattern.matcher(content);
                while(matcher.find()){
                    Paper paper = new Paper(matcher.group(1), 0);
                    paperbox.list.add(paper);
                }
                paperlist.add(paperbox);
            }
        }
        input.close();
        for(int i = 0; i < examlist.size(); i ++){
            if(examlist.get(i).getAllScore()!=100){
                System.out.println(examlist.get(i).toCheck());
            }
        }
        for(int i = 0; i < paperlist.size(); i ++){
            int flag = 0;
            for(int j = 0; j < examlist.size(); j ++){
                if(paperlist.get(i).getName().equals(examlist.get(j).getName())){
                    flag = 1;
                    int mysum = 0;
                    int l = 0;
                    for(l = 0; l < paperlist.get(i).list.size(); l ++){
                        if(paperlist.get(i).list.get(l).getMyAnswer().equals(examlist.get(j).list.get(l).getExamAnswer())){
                            examlist.get(j).list.get(l).setEachScore(examlist.get(j).list.get(l).getScore());
                            mysum +=examlist.get(j).list.get(l).getScore();
                            examlist.get(j).setMyScore(mysum);
                            System.out.println(examlist.get(j).list.get(l).getExamQuestion()+"~"+paperlist.get(i).list.get(l).getMyAnswer()+"~"+"true");
                        }
                        else{
                            System.out.println(examlist.get(j).list.get(l).getExamQuestion()+"~"+paperlist.get(i).list.get(l).getMyAnswer()+"~"+"false");
                        }
                    }
                    while(l<examlist.get(j).list.size()){
                        System.out.println("answer is null");
                        l ++;
                    }
                    for(int k = 0; k <examlist.get(j).list.size(); k ++){
                        System.out.print(examlist.get(j).list.get(k).getEachScore());
                        if(k!=examlist.get(j).list.size()-1){
                            System.out.print(" ");
                        }
                    }
                    System.out.println("~"+examlist.get(j).getMyScore());
                }
            }
            if(flag == 0){
                System.out.println("The test paper number does not exist");
            }
        }
    }
}
  1. 完整代码

先创建三个动态数组,用于存储题目信息,试卷信息,答卷信息。通过一个直到输入end才跳出的循环来进行输入数据的不断接收。在其中使用正则表达式来使输入信息进行

匹配和存储。在答类中包含着一个答题类的动态数组,试卷类中包含一个试题类的动态数组。输入的题目信息存入题目集中,输入的试卷信息存入试卷集中,输入的答题信息

存入答卷集中,在完成输入后进行一个总分的计算。试卷的信息通过题号信息来找到对应的题目信息。答卷中也通过对所输入答案进行与题目相同的试卷答案进行比对。比对

方法采用字符串的比对方法,相同返回true,不同返回false。

完整代码
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main{
    public static void main(String[] args) {
        ArrayList<Project> list = new ArrayList<Project>();
        ArrayList<ExamBox> examlist = new ArrayList<ExamBox>();
        ArrayList<PaperBox> paperlist = new ArrayList<PaperBox>();
        Scanner input = new Scanner(System.in);
        String content = "null";
        while(!content.equals("end")){
            content = input.nextLine();
            if(content.charAt(1) == 'N'){
                Pattern pattern = Pattern.compile("#N:(\\w+) #Q:(.+) #A:(.+)");
                Matcher matcher = pattern.matcher(content);
                Project project = null;
                while(matcher.find()){
                    project = new Project(matcher.group(1),matcher.group(2),matcher.group(3));
                }
                list.add(project);
            }
            else if (content.charAt(1) == 'T'){
                Pattern pattern = Pattern.compile("#T:(\\w+)");
                Matcher matcher = pattern.matcher(content);
                ExamBox exambox = null;
                while(matcher.find()){
                    exambox = new ExamBox(matcher.group(1), 0);
                }
                pattern = Pattern.compile("([\\u4E00-\\u9FA5A-Za-z0-9_]+)-([\\u4E00-\\u9FA5A-Za-z0-9_]+)");
                matcher = pattern.matcher(content);
                int sum = 0;
                while(matcher.find()){
                    sum += Integer.parseInt(matcher.group(2));
                    // System.out.println(Integer.parseInt(matcher.group(2)));
                    Exam exam = new Exam(matcher.group(1), Integer.parseInt(matcher.group(2)));
                    exambox.list.add(exam);
                }
                exambox.setAllScore(sum);
                for(int i = 0; i < exambox.list.size(); i ++){
                    for(int j = 0; j < list.size(); j ++){
                        if(exambox.list.get(i).getExamNum().equals(list.get(j).getNum())){
                            exambox.list.get(i).setExamAnswer(list.get(j).getAnswer());
                            exambox.list.get(i).setExamQuestion(list.get(j).getQuestion());
                        }
                    }
                }
                examlist.add(exambox);
            }
            else if (content.charAt(1) == 'S'){
                Pattern pattern = Pattern.compile("#S:(\\w+)");
                Matcher matcher = pattern.matcher(content);
                PaperBox paperbox = null;
                while(matcher.find()){
                    paperbox = new PaperBox(matcher.group(1), 0);
                }
                pattern = Pattern.compile("#A:([\\u4E00-\\u9FA5A-Za-z0-9_]+)");
                matcher = pattern.matcher(content);
                while(matcher.find()){
                    Paper paper = new Paper(matcher.group(1), 0);
                    paperbox.list.add(paper);
                }
                paperlist.add(paperbox);
            }
        }
        input.close();
        for(int i = 0; i < examlist.size(); i ++){
            if(examlist.get(i).getAllScore()!=100){
                System.out.println(examlist.get(i).toCheck());
            }
        }
        for(int i = 0; i < paperlist.size(); i ++){
            int flag = 0;
            for(int j = 0; j < examlist.size(); j ++){
                if(paperlist.get(i).getName().equals(examlist.get(j).getName())){
                    flag = 1;
                    int mysum = 0;
                    int l = 0;
                    for(l = 0; l < paperlist.get(i).list.size(); l ++){
                        if(paperlist.get(i).list.get(l).getMyAnswer().equals(examlist.get(j).list.get(l).getExamAnswer())){
                            examlist.get(j).list.get(l).setEachScore(examlist.get(j).list.get(l).getScore());
                            mysum +=examlist.get(j).list.get(l).getScore();
                            examlist.get(j).setMyScore(mysum);
                            System.out.println(examlist.get(j).list.get(l).getExamQuestion()+"~"+paperlist.get(i).list.get(l).getMyAnswer()+"~"+"true");
                        }
                        else{
                            System.out.println(examlist.get(j).list.get(l).getExamQuestion()+"~"+paperlist.get(i).list.get(l).getMyAnswer()+"~"+"false");
                        }
                    }
                    while(l<examlist.get(j).list.size()){
                        System.out.println("answer is null");
                        l ++;
                    }
                    for(int k = 0; k <examlist.get(j).list.size(); k ++){
                        System.out.print(examlist.get(j).list.get(k).getEachScore());
                        if(k!=examlist.get(j).list.size()-1){
                            System.out.print(" ");
                        }
                    }
                    System.out.println("~"+examlist.get(j).getMyScore());
                }
            }
            if(flag == 0){
                System.out.println("The test paper number does not exist");
            }
        }
    }
}

class Project{
    private String num;
    private String question;
    private String answer;
    Project (String num,String question,String answer){
        this.num = num;
        this.question = question;
        this.answer = answer;
    }
    public String getNum(){
        return num;
    }
    public String getQuestion(){
        return question;
    }
    public String getAnswer(){
        return answer;
    }
    public void setNum(String num){
        this.num = num;
    }
    public void setQuestion(String question){
        this.question = question;
    }
    public void setAnswer(String answer){
        this.answer = answer;
    }
}

class ExamBox{
    private String name;
    private int allScore = 0;
    private int myscore = 0;
    ArrayList<Exam> list = new ArrayList<Exam>();
    ExamBox(String name,int allScore){
        this.name = name;
        this.allScore = allScore;
    }
    public String getName(){
        return name;
    }
    public int getAllScore(){
        return allScore;
    }
    public int getMyScore(){
        return myscore;
    }
    public void setName(String name){
        this.name = name;
    }
    public void setAllScore(int allScore){
        this.allScore = allScore;
    }
    public void setMyScore(int myscore){
        this.myscore = myscore;
    }
    public String toCheck(){
        return "alert: full score of test paper"+getName()+" is not 100 points";
    }
}

class Exam{
    private String examanswer;
    private String examnum;
    private String examquestion;
    private int score;
    private int eachscore = 0;
    Exam(String examnum, int score){
        this.examnum = examnum;
        this.score = score;
    }
    public String getExamQuestion(){
        return examquestion;
    }
    public String getExamAnswer(){
        return examanswer;
    }
    public String getExamNum(){
        return examnum;
    }
    public int getScore(){
        return score;
    }
    public int getEachScore(){
        return eachscore;
    }
    public void setExamAnswer(String examanswer){
        this.examanswer = examanswer;
    }
    public void setExamQuestion(String examquestion){
        this.examquestion = examquestion;
    }
    public void setExamNum(String examnum){
        this.examnum = examnum;
    }
    public void setScore(int score){
        this.score = score;
    }
    public void setEachScore(int eachscore){
        this.eachscore = eachscore;
    }
}

class PaperBox{
    private String name;
    ArrayList<Paper> list = new ArrayList<Paper>();
    PaperBox(String name,int myscore){
        this.name = name;
    }
    public String getName(){
        return name;
    }
    public void setName(String name){
        this.name = name;
    }
}

class Paper{
    private String myanswer;
    Paper(String myanswer,int eachscore){
        this.myanswer = myanswer;
    }
    public String getMyAnswer(){
        return myanswer;
    }
    public void setMyAnswer(String myanswer){
        this.myanswer = myanswer;
    }
}

第三次作业

  1. 题目类
    包含内容:题号(String),问题(String),答案(String),是否存在(boolean)。
    存储题目对应信息。
class Project{
    private String questionNum;
    private String question;
    private String answer;
    private boolean isExist = true;
    Project(String questionNum, String question, String answer) {
        this.answer = answer;
        this.question = question;
        this.questionNum = questionNum;
    }
    public String getAnswer() {
        return answer;
    }
    public String getQuestion() {
        return question;
    }
    public String getQuestionNum() {
        return questionNum;
    }
    public boolean getIsExist(){
        return isExist;
    }
    public void setAnswer(String answer) {
        this.answer = answer;
    }
    public void setExist(boolean isExist) {
        this.isExist = isExist;
    }
    public void setQuestion(String question) {
        this.question = question;
    }
    public void setQuestionNum(String questionNum) {
        this.questionNum = questionNum;
    }
}
  1. 试题类
    包含内容:顺序号(String),题目号(String),对应得分(int),答案正确(boolean),试题是否删除(boolean),试题是否存在(boolean)。
    存储试题信息。
class Exam{
    private String orderNum;
    private String questionNum;
    private int score;
    private boolean isAnswerTrue = false;
    private boolean isExist = true;
    private boolean isQuestionMapExist = false;
    Exam(String orderNum,String questionNum,int score){
        this.orderNum = orderNum;
        this.questionNum = questionNum;
        this.score = score;
    }
    public String getOrderNum() {
        return orderNum;
    }
    public String getQuestionNum() {
        return questionNum;
    }
    public int getScore() {
        return score;
    }
    public boolean getIsExist(){
        return isExist;
    }
    public boolean getIsAnswerTrue(){
        return isAnswerTrue;
    }
    public boolean getIsQuestionMapExist(){
        return isQuestionMapExist;
    }
    public void setAnswerTrue(boolean isAnswerTrue) {
        this.isAnswerTrue = isAnswerTrue;
    }
    public void setExist(boolean isExist) {
        this.isExist = isExist;
    }
    public void setQuestionMapExist(boolean isQuestionMapExist) {
        this.isQuestionMapExist = isQuestionMapExist;
    }
    public void setOrderNum(String orderNum) {
        this.orderNum = orderNum;
    }
    public void setQuestionNum(String questionNum) {
        this.questionNum = questionNum;
    }
    public void setScore(int score) {
        this.score = score;
    }
}
  1. 试卷类
    包含内容:试卷名(String),总分(int),试题哈希表(HashMap)。
    存储试卷信息。
class ExamPaper{
    private String name;
    private int allScore = 0;
    private HashMap<String, Exam> examMap = new HashMap<>();
    ExamPaper(String name){
        this.name = name;
    }
    public void calculateAllScore(){
        int allScore = 0;
        for(String key:examMap.keySet()){
            if(examMap.get(key).getIsExist()){
                allScore += examMap.get(key).getScore();
            }
        }
        setAllScore(allScore);
    }
    public int getAllScore() {
        return allScore;
    }
    public HashMap<String, Exam> getExamMap() {
        return examMap;
    }
    public String getName() {
        return name;
    }
    public void setAllScore(int allScore) {
        this.allScore = allScore;
    }
    public void setExamMap(HashMap<String, Exam> examMap) {
        this.examMap = examMap;
    }
    public void setName(String name) {
        this.name = name;
    }
}
  1. 答题类
    包含内容:顺序号(String),我的答案(String)。
    答题内容的存储。
class Answer{
    private String orderNum;
    private String answer;
    Answer(String orderNum,String answer){
        this.answer = answer;
        this.orderNum = orderNum;
    }
    public String getAnswer() {
        return answer;
    }
    public String getOrderNum() {
        return orderNum;
    }
    public void setAnswer(String answer) {
        this.answer = answer;
    }
    public void setOrderNum(String orderNum) {
        this.orderNum = orderNum;
    }
}
  1. 答卷类
    包含内容:答卷名(String),学生ID(String),总得分(int),答题哈希表(HashMap)。
class AnswerPaper{
    private String name;
    private String id;
    private int allMyScoer = 0;
    private HashMap<String, Answer> answerMap = new HashMap<>();
    AnswerPaper(String name,String id){
        this.id = id;
        this.name = name;
    }
    public int getAllMyScoer() {
        return allMyScoer;
    }
    public HashMap<String, Answer> getAnswerMap() {
        return answerMap;
    }
    public String getId() {
        return id;
    }
    public String getName() {
        return name;
    }
    public void setAllMyScoer(int allMyScoer) {
        this.allMyScoer = allMyScoer;
    }
    public void setAnswerMap(HashMap<String, Answer> answerMap) {
        this.answerMap = answerMap;
    }
    public void setId(String id) {
        this.id = id;
    }
    public void setName(String name) {
        this.name = name;
    }
}
  1. 句子处理类
    包含内容:区分对应的处理方法,N处理方法,T处理方法,X处理方法,S处理方法,D处理方法。
class SentenceProcess{
    SentenceProcess(){}
    public void separateType(String content, HashMap<String, String> studentMap,
            HashMap<String, Project> questionMap, HashMap<String, ExamPaper> examMap,
            HashMap<String, AnswerPaper> paperMap) {
        if (content.charAt(1) == 'N') {
            sentenceprocessN(content, questionMap);
        } else if (content.charAt(1) == 'T') {
            sentenceprocessT(content, examMap,questionMap);
        } else if (content.charAt(1) == 'X') {
            sentenceprocessX(content, studentMap);
        } else if (content.charAt(1) == 'S') {
            sentenceprocessS(content, paperMap);
        } else if (content.charAt(1) == 'D') {
            sentenceprocessD(content, questionMap, examMap);
        }
    }
    public void sentenceprocessN(String content, HashMap<String, Project> questionMap){
        Pattern pattern = Pattern.compile("#N:(\\w+) #Q:(.+) #A:(.+)");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            Project project = new Project(matcher.group(1),matcher.group(2), matcher.group(3));
            questionMap.put(matcher.group(1), project);
        }
    }
    public void sentenceprocessT(String content, HashMap<String, ExamPaper> examMap, HashMap<String, Project> questionMap) {
        Pattern pattern = Pattern.compile("#T:(\\w+)");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            int cnt = 1;
            String name = matcher.group(1);
            ExamPaper exampaper = new ExamPaper(name);
            pattern = Pattern.compile("([\\u4E00-\\u9FA5A-Za-z0-9_]+)-([\\u4E00-\\u9FA5A-Za-z0-9_]+)");
            matcher = pattern.matcher(content);
            while (matcher.find()) {
                Exam exam = new Exam( String.valueOf(cnt),matcher.group(1),Integer.parseInt(matcher.group(2)));
                if(questionMap.containsKey(matcher.group(1))){
                    exam.setQuestionMapExist(true);
                }
                exampaper.getExamMap().put(matcher.group(1), exam);
                cnt++;
            }
            examMap.put(name, exampaper);
        }
    }
    public void sentenceprocessX(String content, HashMap<String, String> studentMap) {
        Pattern pattern = Pattern.compile("(\\w+) (\\w+)");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            studentMap.put(matcher.group(1), matcher.group(2));
        }
    }
    public void sentenceprocessS(String content, HashMap<String, AnswerPaper> paperMap) {
        Pattern pattern = Pattern.compile("#S:(\\w+) (\\w+)");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            String name = matcher.group(1);
            AnswerPaper answerpaper = new AnswerPaper(matcher.group(1),matcher.group(2));
            pattern = Pattern.compile(" #A:(\\w+)-(\\w+)");
            matcher = pattern.matcher(content);
            while (matcher.find()) {
                Answer answer = new Answer(matcher.group(1),matcher.group(2));
                answerpaper.getAnswerMap().put(matcher.group(1), answer);
            }
            paperMap.put(name, answerpaper);
        }
    }

    public void sentenceprocessD(String content, HashMap<String, Project> questionMap,
            HashMap<String, ExamPaper> examMap) {
        Pattern pattern = Pattern.compile("#D:N-(\\w+)");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()){
            if(questionMap.containsKey(matcher.group(1))){
                questionMap.get(matcher.group(1)).setExist(false);
                for(String key :examMap.keySet()){
                    if(examMap.get(key).getExamMap().containsKey(matcher.group(1))){
                        examMap.get(key).getExamMap().get(matcher.group(1)).setExist(false);
                    }
                }
            }
        }
    }
}
  1. 比对类
    包含内容:确认输入格式正确,比对答案正确。
class Check{
    Check(){}
    public boolean checkString(String content) {
        String regstr = "(#N:(\\w+) #Q:(.+) #A:(\\w+))|" +
                "(#D:N-(\\w+))|" +
                "(#T:(\\w+)( (\\w+)-(\\w+))*)|" +
                "(#X:(((\\w+) (\\w+))-?)*)|" +
                "(#S:(\\w+) (\\w+)( #A:(\\w+)-(\\w+))*)|" +
                "end";
        Pattern pattern = Pattern.compile(regstr);
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            return true;
        }
        return false;
    }

    public void checkAnswer(HashMap<String, String> studentMap, HashMap<String, Project> questionMap,
            HashMap<String, ExamPaper> examMap, HashMap<String, AnswerPaper> paperMap) {
        for(String key :paperMap.keySet()){
            if(examMap.containsKey(key)){
                int score = 0;
                for(String keyExam:examMap.get(key).getExamMap().keySet()){
                    if(paperMap.get(key).getAnswerMap().containsKey(examMap.get(key).getExamMap().get(keyExam).getOrderNum())){
                        //is答案存在
                        if(examMap.get(key).getExamMap().get(keyExam).getIsQuestionMapExist()){//题目集中存在
                            if(examMap.get(key).getExamMap().get(keyExam).getIsExist()){//题目是否删除
                                String questionNumber = examMap.get(key).getExamMap().get(keyExam).getQuestionNum();
                                String orderNumber = examMap.get(key).getExamMap().get(keyExam).getOrderNum();
                                if(questionMap.get(questionNumber).getAnswer().equals(paperMap.get(key).getAnswerMap().get(orderNumber).getAnswer())){//题目答案比对
                                    examMap.get(key).getExamMap().get(keyExam).setAnswerTrue(true);
                                    score += examMap.get(key).getExamMap().get(keyExam).getScore();
                                    System.out.println(questionMap.get(questionNumber).getQuestion()+"~"+paperMap.get(key).getAnswerMap().get(orderNumber).getAnswer()+"~true");
                                }
                                else{
                                    System.out.println(questionMap.get(questionNumber).getQuestion()+"~"+paperMap.get(key).getAnswerMap().get(orderNumber).getAnswer()+"~false");
                                }
                            }
                            else{
                                System.out.println("the question " + keyExam + " invalid~0");
                            }
                        }
                        else{//题目集中不存在
                            System.out.println("non-existent question~0");
                        }
                    }
                    else{
                        System.out.println("answer is null");
                    }
                }
                paperMap.get(key).setAllMyScoer(score);
                if(studentMap.containsKey(paperMap.get(key).getId())){
                    System.out.print(paperMap.get(key).getId()+" "+studentMap.get(paperMap.get(key).getId())+":");
                    for(String keyput:examMap.get(key).getExamMap().keySet()){
                        if(examMap.get(key).getExamMap().get(keyput).getIsAnswerTrue()&&examMap.get(key).getExamMap().get(keyput).getIsExist()){
                            System.out.print(" "+examMap.get(key).getExamMap().get(keyput).getScore());
                        }
                        else{
                            System.out.print(" 0");
                        }
                    }
                    System.out.println("~"+paperMap.get(key).getAllMyScoer());
                }
                else{
                    System.out.println(paperMap.get(key).getId()+" not found");
                }
            }
            else{
                System.out.println("The test paper number does not exist");
            }
        }
    }
}
  1. 主函数
public class Main {
    static HashMap<String, String> studentMap = new HashMap<String, String>();// id name
    static HashMap<String, Project> questionMap = new HashMap<String, Project>();// num project
    static HashMap<String, ExamPaper> examMap = new HashMap<String, ExamPaper>();// name exampaper
    static HashMap<String, AnswerPaper> paperMap = new HashMap<String, AnswerPaper>();// name answerpaper
    static ArrayList<String> wrongStringList = new ArrayList<>();
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        String content = "start";
        SentenceProcess sentenceprocess = new SentenceProcess();
        Check check = new Check();
        while (!content.equals("end")) {
            content = sc.nextLine();
            if (check.checkString(content)) {
                sentenceprocess.separateType(content, studentMap, questionMap, examMap, paperMap);
            }
            else {
                wrongStringList.add(content);
            }
        }
        sc.close();
        for(int i = 0; i<wrongStringList.size(); i ++){
            System.out.println("wrong format:" + wrongStringList.get(i));
        }
        for(String key:examMap.keySet()){
            examMap.get(key).calculateAllScore();
            if (examMap.get(key).getAllScore() != 100) {
                System.out.println("alert: full score of test paper" + key + " is not 100 points");
            }
        }
        check.checkAnswer(studentMap, questionMap, examMap, paperMap);
    }
}
  1. 完整代码

先创建四个哈希表用于存储学生信息,题目信息,答卷信息,试卷信息,和一个动态数组用于存储错误格式的字符串。答卷类中有一个答题信息的哈希表,试卷类中有一个

试题类的哈希表。然后通过一个直到输入end才跳出的循环来进行字符串信息的接收。先通过比对格式的正确性来决定是否进行下一步还是直接存储入。如果正确,进行对

应格式的信息处理。信息处理先获取字符串中第2个位置的字母来确定输入的类型,然后用不同的正则表达式来获取对应的信息,并依照要求存储。之后使用checkAnswer

在先确认key存在后,通过key来获取哈希表中的信息来进行比对和处理,最后返回相应的信息。

完整代码
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
    static HashMap<String, String> studentMap = new HashMap<String, String>();// id name
    static HashMap<String, Project> questionMap = new HashMap<String, Project>();// num project
    static HashMap<String, ExamPaper> examMap = new HashMap<String, ExamPaper>();// name exampaper
    static HashMap<String, AnswerPaper> paperMap = new HashMap<String, AnswerPaper>();// name answerpaper
    static ArrayList<String> wrongStringList = new ArrayList<>();
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        String content = "start";
        SentenceProcess sentenceprocess = new SentenceProcess();
        Check check = new Check();
        while (!content.equals("end")) {
            content = sc.nextLine();
            if (check.checkString(content)) {
                sentenceprocess.separateType(content, studentMap, questionMap, examMap, paperMap);
            }
            else {
                wrongStringList.add(content);
            }
        }
        sc.close();
        for(int i = 0; i<wrongStringList.size(); i ++){
            System.out.println("wrong format:" + wrongStringList.get(i));
        }
        for(String key:examMap.keySet()){
            examMap.get(key).calculateAllScore();
            if (examMap.get(key).getAllScore() != 100) {
                System.out.println("alert: full score of test paper" + key + " is not 100 points");
            }
        }
        check.checkAnswer(studentMap, questionMap, examMap, paperMap);
    }
}

class SentenceProcess{
    SentenceProcess(){}
    public void separateType(String content, HashMap<String, String> studentMap,
            HashMap<String, Project> questionMap, HashMap<String, ExamPaper> examMap,
            HashMap<String, AnswerPaper> paperMap) {
        if (content.charAt(1) == 'N') {
            sentenceprocessN(content, questionMap);
        } else if (content.charAt(1) == 'T') {
            sentenceprocessT(content, examMap,questionMap);
        } else if (content.charAt(1) == 'X') {
            sentenceprocessX(content, studentMap);
        } else if (content.charAt(1) == 'S') {
            sentenceprocessS(content, paperMap);
        } else if (content.charAt(1) == 'D') {
            sentenceprocessD(content, questionMap, examMap);
        }
    }
    public void sentenceprocessN(String content, HashMap<String, Project> questionMap){
        Pattern pattern = Pattern.compile("#N:(\\w+) #Q:(.+) #A:(.+)");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            Project project = new Project(matcher.group(1),matcher.group(2), matcher.group(3));
            questionMap.put(matcher.group(1), project);
        }
    }
    public void sentenceprocessT(String content, HashMap<String, ExamPaper> examMap, HashMap<String, Project> questionMap) {
        Pattern pattern = Pattern.compile("#T:(\\w+)");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            int cnt = 1;
            String name = matcher.group(1);
            ExamPaper exampaper = new ExamPaper(name);
            pattern = Pattern.compile("([\\u4E00-\\u9FA5A-Za-z0-9_]+)-([\\u4E00-\\u9FA5A-Za-z0-9_]+)");
            matcher = pattern.matcher(content);
            while (matcher.find()) {
                Exam exam = new Exam( String.valueOf(cnt),matcher.group(1),Integer.parseInt(matcher.group(2)));
                if(questionMap.containsKey(matcher.group(1))){
                    exam.setQuestionMapExist(true);
                }
                exampaper.getExamMap().put(matcher.group(1), exam);
                cnt++;
            }
            examMap.put(name, exampaper);
        }
    }
    public void sentenceprocessX(String content, HashMap<String, String> studentMap) {
        Pattern pattern = Pattern.compile("(\\w+) (\\w+)");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            studentMap.put(matcher.group(1), matcher.group(2));
        }
    }
    public void sentenceprocessS(String content, HashMap<String, AnswerPaper> paperMap) {
        Pattern pattern = Pattern.compile("#S:(\\w+) (\\w+)");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            String name = matcher.group(1);
            AnswerPaper answerpaper = new AnswerPaper(matcher.group(1),matcher.group(2));
            pattern = Pattern.compile(" #A:(\\w+)-(\\w+)");
            matcher = pattern.matcher(content);
            while (matcher.find()) {
                Answer answer = new Answer(matcher.group(1),matcher.group(2));
                answerpaper.getAnswerMap().put(matcher.group(1), answer);
            }
            paperMap.put(name, answerpaper);
        }
    }

    public void sentenceprocessD(String content, HashMap<String, Project> questionMap,
            HashMap<String, ExamPaper> examMap) {
        Pattern pattern = Pattern.compile("#D:N-(\\w+)");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()){
            if(questionMap.containsKey(matcher.group(1))){
                questionMap.get(matcher.group(1)).setExist(false);
                for(String key :examMap.keySet()){
                    if(examMap.get(key).getExamMap().containsKey(matcher.group(1))){
                        examMap.get(key).getExamMap().get(matcher.group(1)).setExist(false);
                    }
                }
            }
        }
    }
}

class Check{
    Check(){}
    public boolean checkString(String content) {
        String regstr = "(#N:(\\w+) #Q:(.+) #A:(\\w+))|" +
                "(#D:N-(\\w+))|" +
                "(#T:(\\w+)( (\\w+)-(\\w+))*)|" +
                "(#X:(((\\w+) (\\w+))-?)*)|" +
                "(#S:(\\w+) (\\w+)( #A:(\\w+)-(\\w+))*)|" +
                "end";
        Pattern pattern = Pattern.compile(regstr);
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            return true;
        }
        return false;
    }

    public void checkAnswer(HashMap<String, String> studentMap, HashMap<String, Project> questionMap,
            HashMap<String, ExamPaper> examMap, HashMap<String, AnswerPaper> paperMap) {
        for(String key :paperMap.keySet()){
            if(examMap.containsKey(key)){
                int score = 0;
                for(String keyExam:examMap.get(key).getExamMap().keySet()){
                    if(paperMap.get(key).getAnswerMap().containsKey(examMap.get(key).getExamMap().get(keyExam).getOrderNum())){
                        //is答案存在
                        if(examMap.get(key).getExamMap().get(keyExam).getIsQuestionMapExist()){//题目集中存在
                            if(examMap.get(key).getExamMap().get(keyExam).getIsExist()){//题目是否删除
                                String questionNumber = examMap.get(key).getExamMap().get(keyExam).getQuestionNum();
                                String orderNumber = examMap.get(key).getExamMap().get(keyExam).getOrderNum();
                                if(questionMap.get(questionNumber).getAnswer().equals(paperMap.get(key).getAnswerMap().get(orderNumber).getAnswer())){//题目答案比对
                                    examMap.get(key).getExamMap().get(keyExam).setAnswerTrue(true);
                                    score += examMap.get(key).getExamMap().get(keyExam).getScore();
                                    System.out.println(questionMap.get(questionNumber).getQuestion()+"~"+paperMap.get(key).getAnswerMap().get(orderNumber).getAnswer()+"~true");
                                }
                                else{
                                    System.out.println(questionMap.get(questionNumber).getQuestion()+"~"+paperMap.get(key).getAnswerMap().get(orderNumber).getAnswer()+"~false");
                                }
                            }
                            else{
                                System.out.println("the question " + keyExam + " invalid~0");
                            }
                        }
                        else{//题目集中不存在
                            System.out.println("non-existent question~0");
                        }
                    }
                    else{
                        System.out.println("answer is null");
                    }
                }
                paperMap.get(key).setAllMyScoer(score);
                if(studentMap.containsKey(paperMap.get(key).getId())){
                    System.out.print(paperMap.get(key).getId()+" "+studentMap.get(paperMap.get(key).getId())+":");
                    for(String keyput:examMap.get(key).getExamMap().keySet()){
                        if(examMap.get(key).getExamMap().get(keyput).getIsAnswerTrue()&&examMap.get(key).getExamMap().get(keyput).getIsExist()){
                            System.out.print(" "+examMap.get(key).getExamMap().get(keyput).getScore());
                        }
                        else{
                            System.out.print(" 0");
                        }
                    }
                    System.out.println("~"+paperMap.get(key).getAllMyScoer());
                }
                else{
                    System.out.println(paperMap.get(key).getId()+" not found");
                }
            }
            else{
                System.out.println("The test paper number does not exist");
            }
        }
    }
}

class Project{
    private String questionNum;
    private String question;
    private String answer;
    private boolean isExist = true;
    Project(String questionNum, String question, String answer) {
        this.answer = answer;
        this.question = question;
        this.questionNum = questionNum;
    }
    public String getAnswer() {
        return answer;
    }
    public String getQuestion() {
        return question;
    }
    public String getQuestionNum() {
        return questionNum;
    }
    public boolean getIsExist(){
        return isExist;
    }
    public void setAnswer(String answer) {
        this.answer = answer;
    }
    public void setExist(boolean isExist) {
        this.isExist = isExist;
    }
    public void setQuestion(String question) {
        this.question = question;
    }
    public void setQuestionNum(String questionNum) {
        this.questionNum = questionNum;
    }
}

class Exam{
    private String orderNum;
    private String questionNum;
    private int score;
    private boolean isAnswerTrue = false;
    private boolean isExist = true;
    private boolean isQuestionMapExist = false;
    Exam(String orderNum,String questionNum,int score){
        this.orderNum = orderNum;
        this.questionNum = questionNum;
        this.score = score;
    }
    public String getOrderNum() {
        return orderNum;
    }
    public String getQuestionNum() {
        return questionNum;
    }
    public int getScore() {
        return score;
    }
    public boolean getIsExist(){
        return isExist;
    }
    public boolean getIsAnswerTrue(){
        return isAnswerTrue;
    }
    public boolean getIsQuestionMapExist(){
        return isQuestionMapExist;
    }
    public void setAnswerTrue(boolean isAnswerTrue) {
        this.isAnswerTrue = isAnswerTrue;
    }
    public void setExist(boolean isExist) {
        this.isExist = isExist;
    }
    public void setQuestionMapExist(boolean isQuestionMapExist) {
        this.isQuestionMapExist = isQuestionMapExist;
    }
    public void setOrderNum(String orderNum) {
        this.orderNum = orderNum;
    }
    public void setQuestionNum(String questionNum) {
        this.questionNum = questionNum;
    }
    public void setScore(int score) {
        this.score = score;
    }
}

class ExamPaper{
    private String name;
    private int allScore = 0;
    private HashMap<String, Exam> examMap = new HashMap<>();
    ExamPaper(String name){
        this.name = name;
    }
    public void calculateAllScore(){
        int allScore = 0;
        for(String key:examMap.keySet()){
            if(examMap.get(key).getIsExist()){
                allScore += examMap.get(key).getScore();
            }
        }
        setAllScore(allScore);
    }
    public int getAllScore() {
        return allScore;
    }
    public HashMap<String, Exam> getExamMap() {
        return examMap;
    }
    public String getName() {
        return name;
    }
    public void setAllScore(int allScore) {
        this.allScore = allScore;
    }
    public void setExamMap(HashMap<String, Exam> examMap) {
        this.examMap = examMap;
    }
    public void setName(String name) {
        this.name = name;
    }
}

class Answer{
    private String orderNum;
    private String answer;
    Answer(String orderNum,String answer){
        this.answer = answer;
        this.orderNum = orderNum;
    }
    public String getAnswer() {
        return answer;
    }
    public String getOrderNum() {
        return orderNum;
    }
    public void setAnswer(String answer) {
        this.answer = answer;
    }
    public void setOrderNum(String orderNum) {
        this.orderNum = orderNum;
    }
}

class AnswerPaper{
    private String name;
    private String id;
    private int allMyScoer = 0;
    private HashMap<String, Answer> answerMap = new HashMap<>();
    AnswerPaper(String name,String id){
        this.id = id;
        this.name = name;
    }
    public int getAllMyScoer() {
        return allMyScoer;
    }
    public HashMap<String, Answer> getAnswerMap() {
        return answerMap;
    }
    public String getId() {
        return id;
    }
    public String getName() {
        return name;
    }
    public void setAllMyScoer(int allMyScoer) {
        this.allMyScoer = allMyScoer;
    }
    public void setAnswerMap(HashMap<String, Answer> answerMap) {
        this.answerMap = answerMap;
    }
    public void setId(String id) {
        this.id = id;
    }
    public void setName(String name) {
        this.name = name;
    }
}

采坑心得

第一次作业

踩坑一

未能对input.nextInt(),后的回车进行处理,导致回车占据了题目的一个位置,导致错误。

解决方案:

增加了String huancun = input.nextLine(); 进行对input.nextInt();后的回车进行吸收处理。使输入的内容位置正确。

第二次作业

踩坑一

未能理解正则表达式中元字符//w+的范围。

解决方案

将Pattern pattern = Pattern.compile("#N:(\w+) #Q:(\w+) #A:(\w+)");中的\w+改为
Pattern pattern = Pattern.compile("#N:(\w+) #Q:(.+) #A:(.+)");

踩坑二

未能将matcher.group()划分的区域规划好

解决方案

将Pattern pattern = Pattern.compile("(#N:(\w+) #Q:(.+) #A:(.+))");重新调整了括号分布,改为了Pattern pattern = Pattern.compile("#N:(\w+) #Q:(.+) #A:(.+)");

第三次作业

踩坑一

字符串错误判断的处理错误

源代码(未改)

public boolean checkString(String content) {
    String regstr = "(#N:(\\w+) #Q:(.+) #A:(\\w+))|" +
           "(#D:N-(\\w+))|" +
           "(#T:(\\w+)( (\\w+)-(\\w+))+)|" +
           "(#X:(((\\w+) (\\w+))-?)+)|" +
           "(#S:(\\w+) (\\w+)( #A:(\\w+)-(\\w+))+)|" +
           "end";
    Pattern pattern = Pattern.compile(regstr);
    Matcher matcher = pattern.matcher(content);
    while (matcher.find()) {
       return true;
    }
    return false;
 }

结果

未能返回目标的对应结果。

解决方案,源代码(改后)

public boolean checkString(String content) {
    String regstr = "(#N:(\\w+) #Q:(.+) #A:(\\w+))|" +
           "(#D:N-(\\w+))|" +
           "(#T:(\\w+)( (\\w+)-(\\w+))*)|" +
           "(#X:(((\\w+) (\\w+))-?)*)|" +
           "(#S:(\\w+) (\\w+)( #A:(\\w+)-(\\w+))*)|" +
           "end";
    Pattern pattern = Pattern.compile(regstr);
    Matcher matcher = pattern.matcher(content);
    while (matcher.find()) {
       return true;
    }
    return false;
 }

踩坑二

错将题目号与顺序号认为是同一个事物。

源代码(未改)

判题程序原本位于AnswerPaper

 public void CalculateMyScore(ExamPaper exampaper,HashMap<String,Project> questionMap,HashMap<String, String> studentMap){
     int score = 0;
     int cnt = 0;
     int checknum = 0;
     for(String key:answerMap.keySet()){
         cnt ++;
         if(exampaper.getExamMap().containsKey(key)){//答卷号与试卷号匹配成功
             if(questionMap.containsKey(key)&&exampaper.getExamMap().get(key).getExist()){//答卷号存在
                 checknum ++;
                 if(questionMap.get(key).getAnswer().equals(answerMap.get(key).getMyAnswer())){
                     answerMap.get(key).setScore(exampaper.getExamMap().get(key).getScore());
                     score += exampaper.getExamMap().get(key).getScore();
                     System.out.println(questionMap.get(key).getQuestion()+"~"+answerMap.get(key).getMyAnswer()+"~"+"true");
                 }
                 else{
                     System.out.println(questionMap.get(key).getQuestion()+"~"+answerMap.get(key).getMyAnswer()+"~"+"false");
                 }
             }
             else if(questionMap.containsKey(key) == false){//答卷号不存在
                 System.out.println("non-existent question~0");
             }
             else if(questionMap.containsKey(key)&&exampaper.getExamMap().get(key).getExist() == false){
                 checknum ++;
                 System.out.println("the question "+key+" invalid~0");
             }
         }
         else{//答卷号与试卷号匹配不成功
             System.out.println("the question "+key+" invalid~0");
         }
     }
     if(checknum == 0){
          System.out.println("answer is null");
     }
     setAllMyScore(score);
     if(studentMap.containsKey(getId())){
         System.out.print(getId()+" "+studentMap.get(getId())+":");
         for(String key:answerMap.keySet()){
             System.out.print(" "+answerMap.get(key).getScore());
         }
         if(cnt<exampaper.getExamMap().size()){
             System.out.print(" 0");
         }
         System.out.println("~"+getAllMyScore());
        }
    else{
        System.out.println(getId()+" not found");
    }
}

结果

未能返回目标答案

解决方案,改后代码

创建Check类,将确认答案正确方法放在Check类

public void checkAnswer(HashMap<String, String> studentMap, HashMap<String, Project> questionMap,
    HashMap<String, ExamPaper> examMap, HashMap<String, AnswerPaper> paperMap) {
    for(String key :paperMap.keySet()){
        if(examMap.containsKey(key)){
            int score = 0;
            for(String keyExam:examMap.get(key).getExamMap().keySet()){
                if(paperMap.get(key).getAnswerMap().containsKey(examMap.get(key).getExamMap().get(keyExam).getOrderNum())){
                    //is答案存在
                    if(examMap.get(key).getExamMap().get(keyExam).getIsQuestionMapExist()){//题目集中存在
                        if(examMap.get(key).getExamMap().get(keyExam).getIsExist()){//题目是否删除
                            String questionNumber = examMap.get(key).getExamMap().get(keyExam).getQuestionNum();
                            String orderNumber = examMap.get(key).getExamMap().get(keyExam).getOrderNum();
                            if(questionMap.get(questionNumber).getAnswer().equals(paperMap.get(key).getAnswerMap().get(orderNumber).getAnswer())){//题目答案比对
                                examMap.get(key).getExamMap().get(keyExam).setAnswerTrue(true);
                                score += examMap.get(key).getExamMap().get(keyExam).getScore();
                                System.out.println(questionMap.get(questionNumber).getQuestion()+"~"+paperMap.get(key).getAnswerMap().get(orderNumber).getAnswer()+"~true");
                            }
                            else{
                                System.out.println(questionMap.get(questionNumber).getQuestion()+"~"+paperMap.get(key).getAnswerMap().get(orderNumber).getAnswer()+"~false");
                            }
                        }
                        else{
                            System.out.println("the question " + keyExam + " invalid~0");
                        }
                    }
                    else{//题目集中不存在
                        System.out.println("non-existent question~0");
                    }
                }
                else{
                    System.out.println("answer is null");
                }
            }
            paperMap.get(key).setAllMyScoer(score);
            if(studentMap.containsKey(paperMap.get(key).getId())){
                System.out.print(paperMap.get(key).getId()+" "+studentMap.get(paperMap.get(key).getId())+":");
                for(String keyput:examMap.get(key).getExamMap().keySet()){
                    if(examMap.get(key).getExamMap().get(keyput).getIsAnswerTrue()&&examMap.get(key).getExamMap().get(keyput).getIsExist()){
                        System.out.print(" "+examMap.get(key).getExamMap().get(keyput).getScore());
                    }
                    else{
                        System.out.print(" 0");
                    }
                }
                System.out.println("~"+paperMap.get(key).getAllMyScoer());
            }
            else{
                System.out.println(paperMap.get(key).getId()+" not found");
            }
        }
        else{
            System.out.println("The test paper number does not exist");
        }
    }
}

踩坑三

未考虑到哈希表中key不存在。

错误显示

解决方案

加上<HashMap_name>.containKey(key)先进行key确认。

改进建议

第一次作业

未能处理好类的职责分配。

将职责大部分放在了main函数中,其中将不合法的字符串进行处理的函数放在了main函数中。
分好的类只有进行数据的存储的功能。

只能进行已知题目数量的输入和处理。

数组的长度是固定的,不能将更多的题目纳入题目集中。不能进行更多的题目处理。

循环太多,不能将更多的题目进行处理。

改换字符串的处理方法,减少字符串的处理时间,更能快速处理字符串。以循环的处理方式将会超出处理字符串的时间要求。

第二次作业

main函数中存在了太多的功能

主函数中包含了太多功能,如字符串的匹配与存储,与答案比对。这几个方法本应该分配到各个类之中,减少main函数的长度。

没有将类的职能划分好。

计算得分的功能放在了试卷类中,答卷类只有答案的存储,而试卷类包含了太多的职能,得分,赋分,题目,题号,答案。应该将职能进行重新分配。

第三次作业

比对答案的函数中嵌套太多了。

比对答案的函数中的循环,判断一层层包含的太多,导致这段代码进行功能的迭代更加困难,如果要加上其他的功能难度更加难匹配。

类的职能划分不够合理。

如答卷的内容没能分配好,应该将题目信息录入答卷,得分信息应该录入答卷信息。

未能实现更多人的答卷的情况。

类与类的关系并没能分配好,答卷和试卷的关系未能分配好。

总结

学得的知识

  1. 正则表达式
    正则表达式是一种用于匹配、查找和操作字符串的强大工具。它是一种描述字符串模式的表达式,可以用来进行字符串的匹配、替换、提取等操作
    常用的正则表达式有
* 汉字:^[\u4e00-\u9fa5]{0,}$
* 英文和数字:^[A-Za-z0-9]+$ 或 ^[A-Za-z0-9]{4,40}$
* 由26个英文字母组成的字符串:^[A-Za-z]+$
* 中文、英文、数字包括下划线:^[\u4E00-\u9FA5A-Za-z0-9_]+$
* 中文、英文、数字但不包括下划线等符号:^[\u4E00-\u9FA5A-Za-z0-9]+$ 或 ^[\u4E00-\u9FA5A-Za-z0-9]{2,20}$
* 数字:^[0-9]*$
* 浮点数:^(-?\d+)(\.\d+)?$ 或 ^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$

详细参考表达式表

作为一种进行字符串处理的方式,能够快速进行字符串的比对,正则表达式是一个强大而灵活的工具,可以应用于各种领域和场景,用于处理和操作文本、数据、日志等。
2. 数组
作为常用的数据存储的类型,用于多种数据的存储。数据类型不仅含八大基本数据类型,还包含引用数据类型,如数组里面可以存数组,数组里面可以存放字符串。在Java中数组属于引用类型,即数组是一个对象。
利用循环对数组中的数据进行批量操作,处理好信息。
3. 动态数组
Java动态数组是一种可以任意伸缩数组长度的对象,其中可用的数据类型与数组相同。但与数组不同的是可以进行大小的改变。在Java中比较常用的是ArrayList,ArrayList是javaAPI中自带的java.util.ArrayList。
4. 哈希表
哈希表是一种数据结构,其中数据以数组格式存储。每个数据值都有一个唯一的键值。如果密钥已知,则访问所需数据的速度非常快。因此,插入和搜索操作的速度与数据大小无关。哈希表由一个用于保存数据的数组和用于生成元素所在位置的索引的哈希组成。HashMap 的 key 与 value 类型可以相同也可以不同,根据定义,不受限制。

还需学习的部分

对于面向对象程序设计的能力的提升。
java中的方法功能的学习应用。
类与类的功能分配和对应数据的存储。

最后希望下次老师能够最后在题目集结束后发布出对应测试点的信息,因为有时候真的想不到了。

标签:总结,题目,String,get,NCHU,matcher,int,key,public
From: https://www.cnblogs.com/xysblogs/p/18148707

相关文章

  • blog1 1--3周PTA训练总结
    一.前言:在学习过C语言之后,面向对象的程序设计在本学期如期开启。该课程的编程语言是java,java与所学过的C语言有诸多相似之处,与C语言课程所不同的是,这门课程注重的是面向对象,如果说C语言是语法的学习,那么java就是其实战应用的学习,这门课的学习更让我深刻的感受到比写代码更重要的......
  • OOP答题判题系统1-3次总结
    一.前言1.知识点:(1)面向对象编程(OOP):这三道题目要求设计并实现题目类、试卷类和答卷类等,这涉及到类的定义、属性的封装以及方法的实现,是OOP的核心概念。(2)方式设计:设计并实现用于判题、保存题目、保存答案等功能的方法,这涉及到方法的命名、参数设计以及逻辑实现。(3)数据结构与集合......
  • 前三次答题判题程序总结
    前言:本次作业主要考察类的设计,从第一次作业只需三到五个类就能解决问题到第三次需要六七个类以上才能解决问题,这要求我们在写程序之前必须设计好各个类之间的关系和职责,这需要我们有良好的设计习惯并且遵守面向对象程序设计的原则。这几次作业难度很大,第一次作业200行可以搞定,第......
  • PTA前三次题目集总结
    以下内容是我对PTA三次习题作业最后一题的思路,源码以及总结学到的java知识大多都来自写题目集这些题目对我对java的认知与学习起到了不小的帮助答题判题程序-1题目内容设计实现答题程序,模拟一个小型的测试,要求输入题目信息和答题信息,根据输入题目信息中的标准答案判断答题的......
  • oop-PTA题目集1~3总结
    一、前言在学习了Java最基础的语法以及完成五次oop课前训练集之后,我们终于步入正轨,从符合人类思维的、流程化的,但存在可复用性差等缺点的面向过程的编程方式转入面向对象编程。谈到面向对象,就不得不谈及它的三大特点:封装、继承、多态。而这三次的题目尚未涉及继承和多态,主要是实......
  • 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)面向对象编程:......
  • linux shell 编程学习总结
    1文件和数组1.1读文件并将文件内容保存到数组,遍历数组src.f文件内容./src/xxx_1.md./src/xxx_2.md./src/xxx_3.md./src/xxx_4.md./src/xxx_5.mdrun.sh#!/bin/bash###readflisttoarraysrc_array=()whilereadline;dosrc_array+=("$line")done<$1##......