Java第一次blog

来源:https://www.cnblogs.com/150xyu-/p/18149171
-Advertisement-
Play Games

前言 整理這個官方翻譯的系列,原因是網上大部分的 tomcat 版本比較舊,此版本為 v11 最新的版本。 開源項目 從零手寫實現 tomcat minicat 別稱【嗅虎】心有猛虎,輕嗅薔薇。 系列文章 web server apache tomcat11-01-官方文檔入門介紹 web serv ...


7-1 答題判題程式-1

前言

這些題目主要用到對象與類的處理:
對象是現實世界或抽象概念中的實體在電腦程式中的表示。
類則是具有相同屬性和方法的對象的集合,是創建對象的模板。通過類,我們可以定義一類對象的共同特征和行為。
1.字元串處理:需要對輸入的題目信息和答題信息進行字元串分割、提取和處理,以獲取題目編號、題目內容、標準答案和答題結果等信息。
2.對象和類:需要設計題目類、試卷類和答卷類,使用面向對象的思想來封裝題目信息、試卷信息和答卷信息,以便於管理和操作。
3.集合類:需要使用集合類來保存題目列表、答案列表和判題結果列表,以便於對題目和答案進行管理和操作。
4.輸入輸出:可以考慮從文件中讀取題目信息和答題信息,並將判題結果輸出到數組中,以實現數據的持久化和方便查看。
5.演算法設計:需要設計判題的演算法,即根據標準答案和用戶答案來判斷答題結果是否正確,可以使用字元串比較或其他方法來實現。
題量大難度高

題目1


分數 50
作者 蔡軻
單位 南昌航空大學
設計實現答題程式,模擬一個小型的測試,要求輸入題目信息和答題信息,根據輸入題目信息中的標準答案判斷答題的結果。
輸入格式:
程式輸入信息分三部分:
1、題目數量
格式:整數數值,若超過1位最高位不能為0,
樣例:34
2、題目內容
一行為一道題,可以輸入多行數據。
格式:"#N:"+題號+" "+"#Q:"+題目內容+" "#A:"+標準答案
格式約束:題目的輸入順序與題號不相關,不一定按題號順序從小到大輸入。
樣例:

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

3、答題信息
答題信息按行輸入,每一行為一組答案,每組答案包含第2部分所有題目的解題答案,答案的順序號與題目題號相對應。
格式
" #A:"+答案內容
格式約束:答案數量與第2部分題目的數量相同,答案之間以英文空格分隔。
樣例:
#A:2 #A:78
2是題號為1的題目的答案
78是題號為2的題目的答案
答題信息以一行"end"標記結束,"end"之後的信息忽略。
輸出格式:
1、題目數量
格式:整數數值,若超過1位最高位不能為0,
樣例:34
2、答題信息
一行為一道題的答題信息,根據題目的數量輸出多行數據。
格式:題目內容+" ~"+答案
樣例:

 1+1=~2
 2+2= ~4

3、判題信息
判題信息為一行數據,一條答題記錄每個答案的判斷結果,答案的先後順序與題目題號相對應。
格式:判題結果+" "+判題結果
格式約束:
1、判題結果輸出只能是true或者false,
2、判題信息的順序與輸入答題信息中的順序相同
樣例: true false true
輸入樣例1:
單個題目。例如:

1#N:1 #Q:1+1= #A:2  #A:2
end

輸出樣例1:
在這裡給出相應的輸出。例如:

 1+1=~2
 true

輸入樣例2:
單個題目。例如:

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

輸出樣例2:
在這裡給出相應的輸出。例如:

1+1=~4
false

輸入樣例3:
多個題目。例如:

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

輸出樣例3:
在這裡給出相應的輸出。例如:

1+1=~2
2+2=~4
true true

輸入樣例4:
多個題目。例如:

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

輸出樣例4:
在這裡給出相應的輸出。例如:

1+1=~2
2+2=~2
true false

輸入樣例5:
多個題目,題號順序與輸入順序不同。例如

2
#N:2 #Q:1+1= #A:2
#N:1 #Q:5+5= #A:10
#A:10 #A:2
end

輸出樣例5:
在這裡給出相應的輸出。例如:

5+5=~10
1+1=~2
true true

輸入樣例6:
含多餘的空格符。例如:

1
#N:1 #Q: The starting point of the Long March is #A:ruijin
#A:ruijin
end

輸出樣例6:
在這裡給出相應的輸出。例如:

The starting point of the Long March is~ruijin
true

輸入樣例7:
含多餘的空格符。例如:

1
#N: 1 #Q: 5 +5= #A:10
#A:10
end

輸出樣例7:
在這裡給出相應的輸出。例如:

5 +5=~10
true

設計建議:
以下是針對以上題目要求的設計建議,其中的屬性、方法為最小集,實現代碼中可根據情況添加所需的內容:
題目類(用於封裝單個題目的信息):
屬性:題目編號、題目內容、標準答案-standardAnswer
方法:數據讀寫set\get方法、判題方法(答案-answer):判斷答案-answer是否符合標準答案-standardAnswer
試卷類(用於封裝整套題目的信息)
屬性:題目列表(題目類的對象集合)、題目數量
方法:判題方法(題號-num、答案-answer):判斷答案-answer是否符合對應題號的題目標準答案-standardAnswer
保存題目(題號-num、題目-question):將題目保存到題目列表中,保存位置與num要能對應
答卷類(用於封裝答題信息)
屬性:試卷(試卷類的對象)、答案列表(保存每一題的答案)、判題列表(保存每一題的判題結果true/false)
方法:判題方法(題號-num):判斷答案列表中第num題的結果是否符合試卷中對應題號的題目標準答案
輸出方法(題號-num):按照題目的格式要求,輸出題號為num的題目的內容和答題結果。
保存一個答案(題號-num,答案-answer):保存題號為num的題目的答題結果answer。
代碼長度限制
16 KB 時間限制 400 ms 記憶體限制 64 MB 棧限制 8192 KB C (gcc) 1

分析題目1可知:

一共要設計出三個類:
題目類,試卷類,答卷類;
如以下代碼://有些部分和題目要求不同可自行更改

//問題類
public static class Question {
    private int questionNumber;
    private String questionContent;
    private String standardAnswer;
    public void setQuestionNumber(int questionNumber) {
        this.questionNumber = questionNumber;
    }
    public int getQuestionNumber() {
        return this.questionNumber;
    }
    public void setQuestionContent(String questionContent) {
        this.questionContent = questionContent.trim();
    }
    public String getQuestionContent() {
        return this.questionContent;
    }
    public void setStandardAnswer(String standardAnswer) {
        this.standardAnswer = standardAnswer;
    }
    public String getStandardAnswer() {
        return this.standardAnswer;
    }
}
// 試卷類
public static class ExamPaper {
    private int numQuestions;
    private Question e[];//用於存儲題目
    public void saveQuestion(int numQuestions, Question e[]) {
        this.numQuestions=numQuestions;
        this.e=new Question[e.length];
        for(int i=0;i<e.length;i++)
        {
            this.e[i]=e[i];
        }
        for(int i=0;i<e.length-1;i++)
        {
            for(int j=0;j<e.length-i-1;j++)
            {
                if(this.e[j].questionNumber>this.e[j+1].questionNumber)
                {
                    Question a=this.e[j];
                    this.e[j]=this.e[j+1];
                    this.e[j+1]=a;
                }
            }
        }
    }
    public int getnum(){
        return numQuestions;
    }
}

// 答卷類
public static class AnswerSheet {
    private ExamPaper examPaper;
    private String questionAnther[];//用於存儲學生答案
    private int i=0;//記錄回答的答案個數
    public AnswerSheet(ExamPaper examPaper) {
        this.examPaper = examPaper;
    }
    public void saveAnswer(String answer1, String answer2) {
        questionAnther=new String[this.examPaper.getnum()];
        questionAnther[i]=answer1;
        i++;
        questionAnther[i]=answer2;
        i++;
    }
    public void saveAnswer(String answer1) {
        if(questionAnther==null)
        {
        questionAnther=new String[this.examPaper.getnum()];
        questionAnther[i]=answer1;
        }
        else
        {
        questionAnther[i]=answer1;
        }
        i++;
    }
    public void output() {//輸出結果
        for(int j=0;j<this.examPaper.getnum();j++)
        {
        System.out.println(examPaper.e[j].questionContent+"~"+questionAnther[j]);
        }
        for(int k=0;k<questionAnther.length;k++)
        {
            if(k!=questionAnther.length-1)
            System.out.printf("%s ",examPaper.e[k].getStandardAnswer().equalsIgnoreCase(questionAnther[k]));
            else
            System.out.printf("%s",examPaper.e[k].getStandardAnswer().equalsIgnoreCase(questionAnther[k]));
        }
    }
}

改進1:


private Question e[];可以用hashmap("key:<Integer> questNumber","value:<Question> q");改省很多代碼;
private String questionAnther[];//可以用hashmap("key:<Integer> questNumber","value:<String> questionAnther"),省去AnswerSheet中的i;

讀取輸入代碼1

用正則表達式代碼實現:

for (int i=0;i<numQuestions;i++)//已知題目個數及輸入格式
        {
            String line=p.nextLine();直接讀取輸入代碼;
            Matcher matcher=Pattern.compile("#N:\\ *(\\d+)\\ *#Q:(.+)\\ *#A:(.+)\\ *").matcher(line);//匹配器及捕獲器
            if (matcher.find())
        {
            int questionNumber=Integer.parseInt(matcher.group(1));//捕獲組
            String questionContent=matcher.group(2);
            String questionAnther=matcher.group(3);
            Question a1=new Question();
            a1.setQuestionNumber(questionNumber);
            a1.setQuestionContent(questionContent);
            a1.setStandardAnswer(questionAnther);
            a[i]=a1;
        }
        }
        q.saveQuestion(numQuestions,a);
        AnswerSheet o=new AnswerSheet(q);
        String questionAnther1;
        String questionAnther2;
        if(numQuestions==1)
        {
            String line=p.nextLine();
            Matcher matcher=Pattern.compile("#A:(.+)").matcher(line);
            if (matcher.find())
            {
            questionAnther1=matcher.group(1).trim();
            o.saveAnswer(questionAnther1);
            }
        }
        else
        {
            String line=p.nextLine();
            String Line[]=line.split(" ");//分割出想要的答案等(//s)
            for(int j=0;j<Line.length;j++)
        {
            Matcher matcher=Pattern.compile("#A:(.+)").matcher(Line[j]);
            if (matcher.find())
            {
            questionAnther1=matcher.group(1).trim();
            o.saveAnswer(questionAnther1);
            }
        }
        }

完整代碼1

有問題可自行更改僅供參考

點擊查看代碼
import java.util.Scanner;
import java.util.regex.*;
public class Main {
    public static void main(String[] args) {
        Scanner p=new Scanner(System.in);
        int numQuestions=p.nextInt();
        p.nextLine();
        Question a[]=new Question[numQuestions];
        ExamPaper q=new ExamPaper();
        for (int i=0;i<numQuestions;i++)
        {
            String line=p.nextLine();
            Matcher matcher=Pattern.compile("#N:\\ *(\\d+)\\ *#Q:(.+)\\ *#A:(.+)\\ *").matcher(line);
            if (matcher.find())
        {
            int questionNumber=Integer.parseInt(matcher.group(1));
            String questionContent=matcher.group(2);
            String questionAnther=matcher.group(3);
            Question a1=new Question();
            a1.setQuestionNumber(questionNumber);
            a1.setQuestionContent(questionContent);
            a1.setStandardAnswer(questionAnther);
            a[i]=a1;
        }
        }
        q.saveQuestion(numQuestions,a);
        AnswerSheet o=new AnswerSheet(q);
        String questionAnther1;
        String questionAnther2;
        if(numQuestions==1)
        {
            String line=p.nextLine();
            Matcher matcher=Pattern.compile("#A:(.+)").matcher(line);
            if (matcher.find())
            {
            questionAnther1=matcher.group(1).trim();
            o.saveAnswer(questionAnther1);
            }
        }
        else
        {
            String line=p.nextLine();
            String Line[]=line.split(" ");
            for(int j=0;j<Line.length;j++)
        {
            Matcher matcher=Pattern.compile("#A:(.+)").matcher(Line[j]);
            if (matcher.find())
            {
            questionAnther1=matcher.group(1).trim();
            o.saveAnswer(questionAnther1);
            }
        }
        }
        o.output();
}
//問題類
public static class Question {
    private int questionNumber;
    private String questionContent;
    private String standardAnswer;
    public void setQuestionNumber(int questionNumber) {
        this.questionNumber = questionNumber;
    }
    public int getQuestionNumber() {
        return this.questionNumber;
    }
    public void setQuestionContent(String questionContent) {
        this.questionContent = questionContent.trim();
    }
    public String getQuestionContent() {
        return this.questionContent;
    }
    public void setStandardAnswer(String standardAnswer) {
        this.standardAnswer = standardAnswer;
    }
    public String getStandardAnswer() {
        return this.standardAnswer;
    }
}
// 試卷類
public static class ExamPaper {
    private int numQuestions;
    private Question e[];
    public void saveQuestion(int numQuestions, Question e[]) {
        this.numQuestions=numQuestions;
        this.e=new Question[e.length];
        for(int i=0;i<e.length;i++)
        {
            this.e[i]=e[i];
        }
        for(int i=0;i<e.length-1;i++)
        {
            for(int j=0;j<e.length-i-1;j++)
            {
                if(this.e[j].questionNumber>this.e[j+1].questionNumber)
                {
                    Question a=this.e[j];
                    this.e[j]=this.e[j+1];
                    this.e[j+1]=a;
                }
            }
        }
    }
    public int getnum(){
        return numQuestions;
    }
}

// 答卷類
public static class AnswerSheet {
    private ExamPaper examPaper;
    private String questionAnther[];
    private int i=0;
    public AnswerSheet(ExamPaper examPaper) {
        this.examPaper = examPaper;
    }
    public void saveAnswer(String answer1, String answer2) {
        questionAnther=new String[this.examPaper.getnum()];
        questionAnther[i]=answer1;
        i++;
        questionAnther[i]=answer2;
        i++;
    }
    public void saveAnswer(String answer1) {
        if(questionAnther==null)
        {
        questionAnther=new String[this.examPaper.getnum()];
        questionAnther[i]=answer1;
        }
        else
        {
        questionAnther[i]=answer1;
        }
        i++;
    }
    public void output() {
        for(int j=0;j<this.examPaper.getnum();j++)
        {
        System.out.println(examPaper.e[j].questionContent+"~"+questionAnther[j]);
        }
        for(int k=0;k<questionAnther.length;k++)
        {
            if(k!=questionAnther.length-1)
            System.out.printf("%s ",examPaper.e[k].getStandardAnswer().equalsIgnoreCase(questionAnther[k]));
            else
            System.out.printf("%s",examPaper.e[k].getStandardAnswer().equalsIgnoreCase(questionAnther[k]));
        }
    }
}
}

題目2

7-4 答題判題程式-2
分數 73
困難
作者 蔡軻
單位 南昌航空大學
設計實現答題程式,模擬一個小型的測試,以下粗體字顯示的是在答題判題程式-1基礎上增補或者修改的內容。

要求輸入題目信息、試卷信息和答題信息,根據輸入題目信息中的標準答案判斷答題的結果。

輸入格式:

程式輸入信息分三種,三種信息可能會打亂順序混合輸入:

1、題目信息

一行為一道題,可輸入多行數據(多道題)。

格式:"#N:"+題目編號+" "+"#Q:"+題目內容+" "#A:"+標準答案

格式約束:

1、題目的輸入順序與題號不相關,不一定按題號順序從小到大輸入。
2、允許題目編號有缺失,例如:所有輸入的題號為1、2、5,缺少其中的3號題。此種情況視為正常。

樣例:

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

2、試卷信息

一行為一張試卷,可輸入多行數據(多張捲)。

格式:"#T:"+試卷號+" "+題目編號+"-"+題目分值

 題目編號應與題目信息中的編號對應。

 一行信息中可有多項題目編號與分值。

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

3、答卷信息

答卷信息按行輸入,每一行為一張答卷的答案,每組答案包含某個試卷信息中的題目的解題答案,答案的順序與試卷信息中的題目順序相對應。

格式:"#S:"+試卷號+" "+"#A:"+答案內容

格式約束:答案數量可以不等於試卷信息中題目的數量,沒有答案的題目計0分,多餘的答案直接忽略,答案之間以英文空格分隔。

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

   1是試卷號 

   5是1號試卷的順序第1題的題目答案

   22是1號試卷的順序第2題的題目答案

答題信息以一行"end"標記結束,"end"之後的信息忽略。

輸出格式:

1、試卷總分警示

該部分僅當一張試卷的總分分值不等於100分時作提示之用,試卷依然屬於正常試卷,可用於後面的答題。如果總分等於100分,該部分忽略,不輸出。

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

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

2、答卷信息

一行為一道題的答題信息,根據試卷的題目的數量輸出多行數據。

格式:題目內容+""+答案++""+判題結果(true/false)

約束:如果輸入的答案信息少於試卷的題目數量,答案的題目要輸"answer is null"

樣例:3+2=5true

     4+6=~22~false.

  answer is null

3、判分信息

判分信息為一行數據,是一條答題記錄所對應試卷的每道小題的計分以及總分,計分輸出的先後順序與題目題號相對應。

格式:題目得分+" "+....+題目得分+"~"+總分

格式約束:

1、沒有輸入答案的題目計0分

2、判題信息的順序與輸入答題信息中的順序相同
樣例:5 8 0~13

根據輸入的答卷的數量以上2、3項答卷信息與判分信息將重覆輸出。

4、提示錯誤的試卷號

如果答案信息中試卷的編號找不到,則輸出”the test paper number does not exist”,參見樣例9。

設計建議:

參考答題判題程式-1,建議增加答題類,類的內容以及類之間的關聯自行設計。

輸入樣例1:
一張試卷一張答卷。試卷滿分不等於100。例如:

#N:1 #Q:1+1= #A:2
#N:2 #Q:2+2= #A:4
#T:1 1-5 2-8
#S:1 #A:5 #A:22
end

輸出樣例1:
在這裡給出相應的輸出。例如:

alert: full score of test paper1 is not 100 points
1+1=~5~false
2+2=~22~false
0 0~0

輸入樣例2:
一張試卷一張答卷。試卷滿分不等於100。例如:

#N:1 #Q:1+1= #A:2
#N:2 #Q:2+2= #A:4
#T:1 1-70 2-30
#S:1 #A:5 #A:22
end

輸出樣例2:
在這裡給出相應的輸出。例如:

1+1=~5~false
2+2=~22~false
0 0~0

輸入樣例3:
一張試卷、一張答卷。各類信息混合輸入。例如:

#N:1 #Q:1+1= #A:2
#N:2 #Q:2+2= #A:4
#T:1 1-70 2-30
#N:3 #Q:3+2= #A:5
#S:1 #A:5 #A:4
end

輸出樣例:
在這裡給出相應的輸出。例如:

1+1=~5~false
2+2=~4~true
0 30~30

輸入樣例4:
試卷題目的順序與題號不一致。例如:

#N:1 #Q:1+1= #A:2
#N:2 #Q:2+2= #A:4
#T:1 2-70 1-30
#N:3 #Q:3+2= #A:5
#S:1 #A:5 #A:22
end

輸出樣例:
在這裡給出相應的輸出。例如:

2+2=~5~false
1+1=~22~false
0 0~0

輸入樣例5:
亂序輸入。例如:

#N:3 #Q:3+2= #A:5
#N:2 #Q:2+2= #A:4
#T:1 3-70 2-30
#S:1 #A:5 #A:22
#N:1 #Q:1+1= #A:2
end

輸出樣例:
在這裡給出相應的輸出。例如:

3+2=~5~true
2+2=~22~false
70 0~70

輸入樣例6:
亂序輸入+兩份答卷。例如:

#N:3 #Q:3+2= #A:5
#N:2 #Q:2+2= #A:4
#T:1 3-70 2-30
#S:1 #A:5 #A:22
#N:1 #Q:1+1= #A:2
#S:1 #A:5 #A:4
end

輸出樣例:
在這裡給出相應的輸出。例如:

3+2=~5~true
2+2=~22~false
70 0~70
3+2=~5~true
2+2=~4~true
70 30~100

輸入樣例7:
亂序輸入+分值不足100+兩份答卷。例如:

#N:3 #Q:3+2= #A:5
#N:2 #Q:2+2= #A:4
#T:1 3-7 2-6
#S:1 #A:5 #A:22
#N:1 #Q:1+1= #A:2
#S:1 #A:5 #A:4
end

輸出樣例:
在這裡給出相應的輸出。例如:

alert: full score of test paper1 is not 100 points
3+2=~5~true
2+2=~22~false
7 0~7
3+2=~5~true
2+2=~4~true
7 6~13

輸入樣例8:
亂序輸入+分值不足100+兩份答卷+答卷缺失部分答案。例如:

#N:3 #Q:3+2= #A:5
#N:2 #Q:2+2= #A:4
#T:1 3-7 2-6
#S:1 #A:5 #A:22
#N:1 #Q:1+1= #A:2
#T:2 2-5 1-3 3-2
#S:2 #A:5 #A:4
end

輸出樣例:
在這裡給出相應的輸出。例如:

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

輸入樣例9:
亂序輸入+分值不足100+兩份答卷+無效的試卷號。例如:

#N:3 #Q:3+2= #A:5
#N:2 #Q:2+2= #A:4
#T:1 3-7 2-6
#S:3 #A:5 #A:4
end

輸出樣例:
在這裡給出相應的輸出。例如:

alert: full score of test paper1 is not 100 points
The test paper number does not exist

分析題2目可知:

題目增加了:
1.三種信息可能會打亂順序混合輸入(將讀取的數據存入String數組中)。
2.試卷號可輸入多行數據(多張捲)題目分值不滿100分要輸出內容。
3.判分信息(輸出分數);
三個類如下代碼:有錯誤在內
//問題類

public static class Question {
    private int questionNumber;
    private String questionContent;
    private String standardAnswer;
    private int Score;
    public void setQuestionNumber(int questionNumber) {
        this.questionNumber = questionNumber;
    }
    public void setQuestionScore(int Score) {
        this.Score=Score;
    }
    public int getQuestionScore() {
        return Score;
    }
    public int getQuestionNumber() {
        return this.questionNumber;
    }
    public void setQuestionContent(String questionContent) {
        this.questionContent = questionContent.trim();
    }
    public String getQuestionContent() {
        return this.questionContent;
    }
    public void setStandardAnswer(String standardAnswer) {
        this.standardAnswer = standardAnswer;
    }
    public String getStandardAnswer() {
        return this.standardAnswer;
    }
}
// 試卷類
public static class ExamPaper {
    private int Scoresum=0;//記錄一張試卷的總分
    private int numQuestions=0;
    private int examnumber;
    private Question e[];//按題目1改
    public void saveQuestion(int numQuestions, Question e[]) {
        this.numQuestions=numQuestions;
        this.e=new Question[numQuestions];
        for(int i=0;i<numQuestions;i++)
        {
            this.e[i]=e[i];
        }
        for(int i=0;i<numQuestions-1;i++)
        {
            for(int j=0;j<numQuestions-i-1;j++)
            {
                if(this.e[j].questionNumber>this.e[j+1].questionNumber)
                {
                    Question a=this.e[j];
                    this.e[j]=this.e[j+1];
                    this.e[j+1]=a;
                }
            }
        }
    }
    public int getnum(){
        return numQuestions;
    }
    public void setExamnumber(int examnumber) {
        this.examnumber=examnumber;
    }
    public void setExamsum() {
        for(int i=0;i<e.length;i++)
        Scoresum=Scoresum+e[i].getQuestionScore();
    }
    public int GetExamsum() {
        return Scoresum;
    }
}

// 答卷類
public static class AnswerSheet {
    private ExamPaper examPaper;//寫一個hashmap(<Integer> exmNumber,<ExamPaper> examPaper);
    private int ScoreA=0;//回答者的得分
    private String questionAnther[];//按題目1改
    private int i=0;//去掉
    public AnswerSheet(ExamPaper examPaper) {
        this.examPaper = examPaper;
    }
    public void saveAnswer(String answer1,int sort[]) {
        if(questionAnther==null)
        {
        questionAnther=new String[20];
        questionAnther[sort[i]-1]=answer1;
        }
        else
        {
        questionAnther[sort[i]-1]=answer1;
        }
        i++;
    }
    public int Geti(){
        return i;
    }
    public void output(int sort[]) {
        int a[]=new int [this.examPaper.getnum()];//記錄得分情況也可以加到Question類中
        for(int g=0;g<i;g++)
        {
        for(int k=0;k<i;k++)
        {//改為一個迴圈mapQuestion.get(k).equal(mapAnther.get(k));
            if(k+1==sort[g])
            System.out.println(examPaper.e[k].questionContent+"~"+questionAnther[k]+"~"+examPaper.e[k].getStandardAnswer().equalsIgnoreCase(questionAnther[k]));
        }
        }
        for(int j=0;j<i;j++)
        {//mapQuestion.get(k).GetScore();
        if(examPaper.e[j].getStandardAnswer().equalsIgnoreCase(questionAnther[j]))
        {
            ScoreA+=examPaper.e[j].getQuestionScore();
            a[j]=examPaper.e[j].getQuestionScore();
        }
        }
        if(this.examPaper.getnum()>i)
        {
            for(int k=0;k<this.examPaper.getnum()-i;k++)
            {
                System.out.println("answer is null");
            }
        }
        for(int l=0;l<i;l++)
        {
            if(l!=i-1)
            System.out.printf("%d ",a[l]);
            else
            System.out.printf("%d%c%d",a[l],'~',ScoreA);
        }
    }
}

改進2:

部分改進在代碼中,hashmap中的key可以寫成ArrayList類的集合減少迴圈次數;

List <Integer> keys = new ArrayList <Integer>(map. keySet());//改正
List<String> returnResult2 = new LinkedList<String>();
Collection<String> values =  map.values();
Iterator<String> it2 = values.iterator();
while(it2.hasNext()) {
    returnResult2.add(it2.next());
}

完整代碼2

有問題可自行更改僅供參考。

點擊查看代碼
import java.util.Scanner;
import java.util.regex.*;
public class Main {
    public static void main(String[] args) {
        Scanner p=new Scanner(System.in);
        String e1=p.nextLine();
        int sort[]=new int[10];
        String []esum=new String [50];
        Question a[]=new Question[20];
        int i=0,k=0,j;
        while(!e1.equals("end"))
        {
        esum[i]=e1;
        e1=p.nextLine();
        i++;
        }
        ExamPaper q=new ExamPaper();
        for(j=0;j<i;j++)
        {
            Matcher matcher=Pattern.compile("#N:\\s*(\\d+)\\s*#Q:(.+)#A:(.+)").matcher(esum[j]);
            if(matcher.find())
        {
            int questionNumber=Integer.parseInt(matcher.group(1));
            String questionContent=matcher.group(2);
            String questionAnther=matcher.group(3);
            Question a1=new Question();
            a1.setQuestionNumber(questionNumber);
            a1.setQuestionContent(questionContent);
            a1.setStandardAnswer(questionAnther);
            a[k]=a1;
            k++;
        }
        else
        {
            matcher=Pattern.compile("#T:(\\d+)\\s*(.+)").matcher(esum[j]);
            if (matcher.find()){
            q.setExamnumber(Integer.parseInt(matcher.group(1)));
            String sum5[]=matcher.group(2).split("\\s");
            for (int y=0;y<sum5.length;y++)
            {
                String []sum4=sum5[y].split("-");
                int questionNumber=Integer.parseInt(sum4[0]);
                sort[y]=questionNumber;
                int Score=Integer.parseInt(sum4[1]);
                for(int l=0;l<k;l++)
                {
                    if(a[l].getQuestionNumber()==questionNumber)
                    {
                        a[l].setQuestionScore(Score);
                    }
                }
            }
            j++;
            break;
            }
        }
        }
        q.saveQuestion(k,a);
        q.setExamsum();
        if(q.GetExamsum()!=100)
        {
            System.out.println("alert: full score of test paper1 is not 100 points");
        }
        AnswerSheet o=new AnswerSheet(q);
        AnswerSheet o1=new AnswerSheet(q);
        int numg=0;
        String questionAnther1;
        for (int x=j;x<i;x++)
        {
            Matcher matcher=Pattern.compile("#S:\\s*(\\d+)\\s*(.+)").matcher(esum[x]);
            if(matcher.find())
            {
            int examnumber=Integer.parseInt(matcher.group(1));
            String ranges=matcher.group(2);
            String[] Line=ranges.split("\\s+");
                if(numg==0)
            {
            for(int c=0;c<Line.length;c++)
        {
            matcher=Pattern.compile("#A:(.+)").matcher(Line[c]);
            if (matcher.find())
            {
            questionAnther1=matcher.group(1).trim();
            o.saveAnswer(questionAnther1,sort);
            }
        }
            }
            else
            {
                for(int c=0;c<Line.length;c++)
        {
            matcher=Pattern.compile("#A:(.+)").matcher(Line[c]);
            if (matcher.find())
            {
            questionAnther1=matcher.group(1).trim();
            o1.saveAnswer(questionAnther1,sort);
            }
        }
            }
            }
        }
        o.output(sort);
        if(o1.Geti()!=0)
        o1.output(sort);
}
//問題類
public static class Question {
    private int questionNumber;
    private String questionContent;
    private String standardAnswer;
    private int Score;
    public void setQuestionNumber(int questionNumber) {
        this.questionNumber = questionNumber;
    }
    public void setQuestionScore(int Score) {
        this.Score=Score;
    }
    public int getQuestionScore() {
        return Score;
    }
    public int getQuestionNumber() {
        return this.questionNumber;
    }
    public void setQuestionContent(String questionContent) {
        this.questionContent = questionContent.trim();
    }
    public String getQuestionContent() {
        return this.questionContent;
    }
    public void setStandardAnswer(String standardAnswer) {
        this.standardAnswer = standardAnswer;
    }
    public String getStandardAnswer() {
        return this.standardAnswer;
    }
}
// 試卷類
public static class ExamPaper {
    private int Scoresum=0;
    private int numQuestions=0;
    private int examnumber;
    private Question e[];
    public void saveQuestion(int numQuestions, Question e[]) {
        this.numQuestions=numQuestions;
        this.e=new Question[numQuestions];
        for(int i=0;i<numQuestions;i++)
        {
            this.e[i]=e[i];
        }
        for(int i=0;i<numQuestions-1;i++)
        {
            for(int j=0;j<numQuestions-i-1;j++)
            {
                if(this.e[j].questionNumber>this.e[j+1].questionNumber)
                {
                    Question a=this.e[j];
                    this.e[j]=this.e[j+1];
                    this.e[j+1]=a;
                }
            }
        }
    }
    public int getnum(){
        return numQuestions;
    }
    public void setExamnumber(int examnumber) {
        this.examnumber=examnumber;
    }
    public void setExamsum() {
        for(int i=0;i<e.length;i++)
        Scoresum=Scoresum+e[i].getQuestionScore();
    }
    public int GetExamsum() {
        return Scoresum;
    }
}

// 答卷類
public static class AnswerSheet {
    private ExamPaper examPaper;
    private int ScoreA=0;
    private String questionAnther[];
    private int i=0;
    public AnswerSheet(ExamPaper examPaper) {
        this.examPaper = examPaper;
    }
    public void saveAnswer(String answer1,int sort[]) {
        if(questionAnther==null)
        {
        questionAnther=new String[20];
        questionAnther[sort[i]-1]=answer1;
        }
        else
        {
        questionAnther[sort[i]-1]=answer1;
        }
        i++;
    }
    public int Geti(){
        return i;
    }
    public void output(int sort[]) {
        int a[]=new int [this.examPaper.getnum()];
        for(int g=0;g<i;g++)
        {
        for(int k=0;k<i;k++)
        {
            if(k+1==sort[g])
            System.out.println(examPaper.e[k].questionContent+"~"+questionAnther[k]+"~"+examPaper.e[k].getStandardAnswer().equalsIgnoreCase(questionAnther[k]));
        }
        }
        for(int j=0;j<i;j++)
        {
        if(examPaper.e[j].getStandardAnswer().equalsIgnoreCase(questionAnther[j]))
        {
            ScoreA+=examPaper.e[j].getQuestionScore();
            a[j]=examPaper.e[j].getQuestionScore();
        }
        }
        if(this.examPaper.getnum()>i)
        {
            for(int k=0;k<this.examPaper.getnum()-i;k++)
            {
                System.out.println("answer is null");
            }
        }
        for(int l=0;l<i;l++)
        {
            if(l!=i-1)
            System.out.printf("%d ",a[l]);
            else
            System.out.printf("%d%c%d",a[l],'~',ScoreA);
        }
    }
}
}

題目3

在答題判題程式-2基礎上增補或者修改的內容,要求輸入題目信息、試卷信息、答題信息、學生信息、刪除題目信息,根據輸入題目信息中的標準答案判斷答題的結果。

分析題3目可知:

1.需要添加學生類。
2.刪除題目信息要對應輸出無該題。
3.輸出形式的改變。
4.題目信息有錯要判斷並輸出。
新類代碼:

//學生類
    public static class Student {
    private String studentId;  
    private String name;  
    public Student(String studentId, String name) {  
        this.studentId = studentId;  
        this.name = name;
    }  
  
    // 學號的getter方法  
    public String getStudentId() {  
        return studentId;  
    }  
  
    // 學號的setter方法  
    public void setStudentId(String studentId) {  
        this.studentId = studentId;  
    }  
  
    // 姓名的getter方法  
    public String getName() {  
        return name;  
    }  
  
    // 姓名的setter方法  
    public void setName(String name) {  
        this.name = name;  
    }  
    }

改進3:

可在原代碼中用hashmap(key<String> StuId,value<Student> Stu);
來對應輸入的學號對象;
對matcher讀取的錯誤輸入內容添加處理代碼:

for(j=0;j<i;j++)
        {
            Matcher matcher=Pattern.compile("#N:\\s*(\\d+)\\s*#Q:(.+)#A:(.+)").matcher(esum[j]);
            if(matcher.find())
        {
            int questionNumber=Integer.parseInt(matcher.group(1));
            String questionContent=matcher.group(2);
            String questionAnther=matcher.group(3);
            Question a1=new Question();
            a1.setQuestionNumber(questionNumber);
            a1.setQuestionContent(questionContent);
            a1.setStandardAnswer(questionAnther);
            a[questionNumber]=a1;
            k++;
        }
        else
        {
            matcher=Pattern.compile("#T:(\\d+)\\s*(.+)").matcher(esum[j]);
            if (matcher.find()){
            q.setExamnumber(Integer.parseInt(matcher.group(1)));
            String sum5[]=matcher.group(2).split("\\s");
            for (int y=0;y<sum5.length;y++)
            {
                String []sum4=sum5[y].split("-");
                int questionNumber=Integer.parseInt(sum4[0]);
                int Score=Integer.parseInt(sum4[1]);
                if(a[questionNumber]!=null)
                {
                	a[questionNumber].setQuestionScore(Score);
                	a[questionNumber].setState(true);
                }
                if(a[questionNumber]==null)
                {
                	Question a1=new Question();
                	a[questionNumber]=a1;
                	a[questionNumber].setQuestionScore(Score);
                	a[questionNumber].setState(true);
                }
            }
            j++;
            break;
            }
        }
        }
        q.saveQuestion(k,a);
        q.setExamsum();
        AnswerSheet o=new AnswerSheet(q);
        AnswerSheet o1=new AnswerSheet(q);
        int numg=0;
        String questionAnther1;
        for (int x=j;x<i;x++)
        {
            Matcher matcher=Pattern.compile("#S:\\s*(\\d+)\\s*(.+)").matcher(esum[x]);
            if(matcher.find())
            {
            int examnumber=Integer.parseInt(matcher.group(1));
            String ranges=matcher.group(2);
            String[] Line=ranges.split("\\s+");
                if(numg==0)
            {
            for(int c=0;c<Line.length;c++)
        {
            matcher=Pattern.compile("#A:(.+)").matcher(Line[c]);
            if (matcher.find())
            {
            String consist[]=matcher.group(1).split("-");
            int antherNumber=Integer.parseInt(consist[0]);
            questionAnther1=consist[1];
            o.saveAnswer(questionAnther1,antherNumber);
            }
        }
            }
            else
            {
                for(int c=0;c<Line.length;c++)
        {
            matcher=Pattern.compile("#A:(.+)").matcher(Line[c]);
            if (matcher.find())
            {
            String consist[]=matcher.group(1).split("-");
            int antherNumber=Integer.parseInt(consist[0]);
            questionAnther1=consist[1];
            o1.saveAnswer(questionAnther1,antherNumber);
            }
        }
            }
            }
            else
            {
            matcher=Pattern.compile("#X:\\s*(.+)\\s*").matcher(esum[x]);\\加的內容
            if (matcher.find()){
            String []osum=matcher.group(1).split("-");
            for(int m=0;m<osum.length;m++)
            {
                String osum1[]=osum[m].split("\\s");
                Student s1=new Student(osum1[0],osum1[1]);
                Stu[Stunum]=s1;
                Stunum++;
            }
            }
            }
            matcher=Pattern.compile("#D:N-(\\d+)\\s*").matcher(esum[x]);\\加的內容
            if (matcher.find()){
                int delenumber=Integer.parseInt(matcher.group(1));
                o.examPaper.setErronum(delenumber);
            }
        }

總結

對於List類集合和hashmap和正則表達式和代碼的可重用性可修改性都需要進一步學習與研究
學到了正則表達式的使用,大量處理數據的能力。
理解了Java類、對象、封裝,以及如何使用這些概念來構建程式。
集合框架:學習Java的集合框架,如List、Map等介面及其實現類(如ArrayList、HashMap等),它們用於存儲和管理多個對象。
學到了編寫函數(方法)來封裝複雜的邏輯,並學習常見的設計模式,以優化代碼結構和提高可維護性。
掌握Java在eclipse中程式的測試與調試技巧,使用調試器逐步執行代碼,檢查變數的值,並定位和解決程式中的錯誤。
學習到Java編程語言的基本語法,包括變數聲明、數據類型、運算符、條件語句(if-else)、迴圈語句(for, while)、數組等。


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 1. OSI 7層模型 OSI的7層模型對於大家來說可能不太好理解,所以我們通過一個案例來講解: 假設,你在瀏覽器上輸入了一些關鍵字,內部通過DNS找到對應的IP後,再發送數據時內部會做如下的事: 應用層:規定數據的格式。 "GET /s?wd=你好 HTTP/1.1\r\nHost:www.bai ...
  • 前言 整理這個官方翻譯的系列,原因是網上大部分的 tomcat 版本比較舊,此版本為 v11 最新的版本。 開源項目 從零手寫實現 tomcat minicat 別稱【嗅虎】心有猛虎,輕嗅薔薇。 系列文章 web server apache tomcat11-01-官方文檔入門介紹 web serv ...
  • 目錄1. #include <stdio.h>2.int 整形數據類型3.main 函數4.printf 函數5.return 函數6.”{}”與“;” “Hellow world!"是初學者的第一份代碼,可以說所學萬物代碼之基礎。下麵看一下代碼是如何實現的: 代碼雖然簡潔,但是包含了許多C語言的知 ...
  • Python的下載安裝和環境搭建,以及python的IDE工具PyCharm搭建及常用配置教程,可以讓新人快速上手python的使用。 ...
  • 以下是個人總結的Java常用的十大開源工具類庫,根據具體需求和項目,可以選擇合適的工具類庫來提高開發效率。1. Apache Commons:Apache Commons是一個開源的工具類庫,提供了大量常用的工具類和組件,如StringUtils、IOUtils、CollectionUtils等。2 ...
  • 1.為什麼使用索引 索引是存儲引擎用於快速找到數據記錄的一種數據結構,就好比一本書的目錄部分,通過目錄中找到對應文章的頁碼,便可快速定位到需要的文章。MySQL中的索引也是一樣的道理,進行數據查找時,首先查看查詢條件是否命中某條索引,符合則通過索引查找相關數據,如果不符合則需要全表掃描,即需要一條條 ...
  • 1.前言: 第一次作業難度較大,從無到有的設計,涉及到的主要類有Paper,Question,AnswerPaper,Main,主要題目方向為字元串判斷與字元串處理(提取有效信息),判斷對錯算總分,配合一些Java自帶的數據結構如ArrayList即可快速解決問題,第一次作業是後面作業的基礎,需自行 ...
  • oop三次pta總結 前言 在這學期的java課程學習當中,我已經體會到了java這門語言的重要性了,就從這三次pta題目的設計與思路來說吧(還真的有點小難),特別是每一期pta的最後一題...... 《答題判題程式》,這道題目依次迭代,難度依次上升(如果沒有設計好,基本是寄了),題目不多看幾遍細節 ...
一周排行
    -Advertisement-
    Play Games
  • 1、預覽地址:http://139.155.137.144:9012 2、qq群:801913255 一、前言 隨著網路的發展,企業對於信息系統數據的保密工作愈發重視,不同身份、角色對於數據的訪問許可權都應該大相徑庭。 列如 1、不同登錄人員對一個數據列表的可見度是不一樣的,如數據列、數據行、數據按鈕 ...
  • 前言 上一篇文章寫瞭如何使用RabbitMQ做個簡單的發送郵件項目,然後評論也是比較多,也是準備去學習一下如何確保RabbitMQ的消息可靠性,但是由於時間原因,先來說說設計模式中的簡單工廠模式吧! 在瞭解簡單工廠模式之前,我們要知道C#是一款面向對象的高級程式語言。它有3大特性,封裝、繼承、多態。 ...
  • Nodify學習 一:介紹與使用 - 可樂_加冰 - 博客園 (cnblogs.com) Nodify學習 二:添加節點 - 可樂_加冰 - 博客園 (cnblogs.com) 介紹 Nodify是一個WPF基於節點的編輯器控制項,其中包含一系列節點、連接和連接器組件,旨在簡化構建基於節點的工具的過程 ...
  • 創建一個webapi項目做測試使用。 創建新控制器,搭建一個基礎框架,包括獲取當天日期、wiki的請求地址等 創建一個Http請求幫助類以及方法,用於獲取指定URL的信息 使用http請求訪問指定url,先運行一下,看看返回的內容。內容如圖右邊所示,實際上是一個Json數據。我們主要解析 大事記 部 ...
  • 最近在不少自媒體上看到有關.NET與C#的資訊與評價,感覺大家對.NET與C#還是不太瞭解,尤其是對2016年6月發佈的跨平臺.NET Core 1.0,更是知之甚少。在考慮一番之後,還是決定寫點東西總結一下,也回顧一下.NET的發展歷史。 首先,你沒看錯,.NET是跨平臺的,可以在Windows、 ...
  • Nodify學習 一:介紹與使用 - 可樂_加冰 - 博客園 (cnblogs.com) Nodify學習 二:添加節點 - 可樂_加冰 - 博客園 (cnblogs.com) 添加節點(nodes) 通過上一篇我們已經創建好了編輯器實例現在我們為編輯器添加一個節點 添加model和viewmode ...
  • 前言 資料庫併發,數據審計和軟刪除一直是數據持久化方面的經典問題。早些時候,這些工作需要手寫複雜的SQL或者通過存儲過程和觸發器實現。手寫複雜SQL對軟體可維護性構成了相當大的挑戰,隨著SQL字數的變多,用到的嵌套和複雜語法增加,可讀性和可維護性的難度是幾何級暴漲。因此如何在實現功能的同時控制這些S ...
  • 類型檢查和轉換:當你需要檢查對象是否為特定類型,並且希望在同一時間內將其轉換為那個類型時,模式匹配提供了一種更簡潔的方式來完成這一任務,避免了使用傳統的as和is操作符後還需要進行額外的null檢查。 複雜條件邏輯:在處理複雜的條件邏輯時,特別是涉及到多個條件和類型的情況下,使用模式匹配可以使代碼更 ...
  • 在日常開發中,我們經常需要和文件打交道,特別是桌面開發,有時候就會需要載入大批量的文件,而且可能還會存在部分文件缺失的情況,那麼如何才能快速的判斷文件是否存在呢?如果處理不當的,且文件數量比較多的時候,可能會造成卡頓等情況,進而影響程式的使用體驗。今天就以一個簡單的小例子,簡述兩種不同的判斷文件是否... ...
  • 前言 資料庫併發,數據審計和軟刪除一直是數據持久化方面的經典問題。早些時候,這些工作需要手寫複雜的SQL或者通過存儲過程和觸發器實現。手寫複雜SQL對軟體可維護性構成了相當大的挑戰,隨著SQL字數的變多,用到的嵌套和複雜語法增加,可讀性和可維護性的難度是幾何級暴漲。因此如何在實現功能的同時控制這些S ...