NCHU題目集1~3的總結

谢勇晟發表於2024-04-21

目錄

一 . 前言
二 . 設計與分析
三 . 採坑心得
四 . 改進建議
五 . 總結

前言

知識點總結

題目集一

  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中的方法功能的學習應用。
類與類的功能分配和對應資料的儲存。

最後希望下次老師能夠最後在題目集結束後釋出出對應測試點的資訊,因為有時候真的想不到了。

相關文章