OOP-PTA1~3次大作業
Blog1(記錄答題判斷題目)
第一次PTA
題目如下:
7-1 答題判題程序-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。
1.給出題目數量,輸入對應數量的題目與答案判定正確錯誤與否即可
2.輸出題目與提交的答案和判定的結果即可
一個開始看到這個題,我是覺得并不需要用到類的創建,直接用字符串數組也可以完成,索性就比較早寫完了,不創建類的代碼,聽說后面的題目有延展,又添加了類進行,重新寫了一遍。
這是創建的題目類
點擊查看代碼
class Question {
private String num;
private String quiz;
private String answer;
public Question() {
}
public Question(String num, String quiz, String answer) {
this.num = num;
this.quiz = quiz;
this.answer = answer;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getQuiz() {
return quiz;
}
public void setQuiz(String quiz) {
this.quiz = quiz;
}
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
}
由于第一題的數據比較少,個人認為創建一個類即可完成,所以在創建這個類的基礎上再添加部分代碼,遍歷,讀取并判斷數據即可完實現全部功能
以下時代碼部分:
點擊查看代碼
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
scanner.nextLine();
Question[] nb = new Bba[a];
int[] f = new int[a];
for (int i = 0; i < a; i++) {
String input = scanner.nextLine();
String[] parts = input.split("#");
String n = parts[1].replace("N:", "").trim();
nb[Integer.parseInt(n) - 1] = new Question(n, parts[2], parts[3]);
}
String[] array = new String[a];
for (int i = 0; i < a; i++) {
array[i] = scanner.next();
}
for (int i = 0; i < a; i++) {
String k1 = array[i].substring(3);
String k2 = removeLeadingSpaces(nb[i].getAnswer().substring(2));
if (k1.equals(k2)) {
f[i] = 1;
}
//System.out.println(k1+" "+k2);
String k3 = nb[i].getQuiz().substring(2, nb[i].getQuiz().length() - 1);
String k4 = removeLeadingSpaces(k3);
System.out.println(k4 + "~" + k1);
}
for (int i = 0; i < a; i++) {
if(i!=a-1){
if(f[i]==1){
System.out.printf("true ");
}
else{
System.out.printf("false ");
}
}
else{
if(f[i]==1){
System.out.printf("true");
}
else{
System.out.printf("false");
}
}
}
}
public static String removeLeadingSpaces (String input){
if (input.startsWith(" ")) {
return input.trim();
} else {
return input;
}
}
}
第二次PTA:
要求如下:
設計實現答題程序,模擬一個小型的測試,以下粗體字顯示的是在答題判題程序-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”,
這次多了試卷答卷,因此我選擇添加兩個類以實現代碼
創建的類如下:
點擊查看代碼
class Questionbank {
private String num;
private String question;
private String answer;
public Questionbank(String num, String question, String answer) {
this.num = num;
this.question = question;
this.answer = answer;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
}
class Paper {
private String num;
private int[] questionnum;
private int[] questionscore;
private int totalScore;
public int getTotalScore() {
int totalScore = 0;
for (int i = 0; i < questionnum.length; i++) {
totalScore += questionscore[i];
}
return totalScore;
}
public void setTotalScore(int totalScore) {
this.totalScore = totalScore;
}
public Paper(String num, int[] questionnum, int[] questionscore) {
this.num = num;
this.questionnum = questionnum;
this.questionscore = questionscore;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public int[] getQuestionnum() {
return questionnum;
}
public void setQuestionnum(int[] questionnum) {
this.questionnum = questionnum;
}
public int[] getQuestionscore() {
return questionscore;
}
public void setQuestionscore(int[] questionscore) {
this.questionscore = questionscore;
}
public void answer(Answer[] testPaperAnswers, Questionbank[] questionBanks) {
Answer[] answers = this.getAnswer(testPaperAnswers);
for (int l = 0; l < answers.length; l++) {
this.totalScore=0;
Answer answer = answers[l];
if (answer == null) {
continue;
}
String sum = "";
for (int i = 0; i < questionnum.length; i++) {
boolean hasLast = questionnum.length - 1 == i;
int num = questionnum[i];
Questionbank questionBank = this.getQuestionBank(questionBanks, String.valueOf(num));
String[] answerList = answer.getPaperanswer();
if (answerList.length - 1 < i) {
String delimiter = hasLast ? "~" + this.totalScore : " ";
System.out.println("answer is null");
sum = sum + 0 + delimiter;
} else {
if (answerList[i].equals(questionBank.getAnswer())) {
System.out.println(String.format("%s~%s~%s", questionBank.getQuestion(), answerList[i], true));
this.totalScore = this.questionscore[i] + this.totalScore;
String delimiter = hasLast ? "~" + this.totalScore : " ";
sum = sum + this.questionscore[i] + delimiter;
} else {
System.out.println(String.format("%s~%s~%s", questionBank.getQuestion(), answerList[i], false));
String delimiter = hasLast ? "~" + this.totalScore : " ";
sum = sum + 0 + delimiter;
}
}
}
System.out.println(sum);
}
}
private Answer[] getAnswer(Answer[] testPaperAnswers) {
Answer[] answers = new Answer[10];
int index = 0;
for (int i = 0; i < testPaperAnswers.length; i++) {
Answer testPaperAnswer = testPaperAnswers[i];
if (null == testPaperAnswer) {
continue;
}
if (testPaperAnswer.getPapernum().equals(String.valueOf(this.num))) {
answers[index] = testPaperAnswer;
index++;
}
}
return answers;
}
private Questionbank getQuestionBank(Questionbank[] questionBanks, String num) {
for (Questionbank questionBank : questionBanks) {
if (questionBank.getNum().equals(num)) {
return questionBank;
}
}
return null;
}
}
class Answer {
private String papernum;
private String[] paperanswer;
private int totalscore;
public Answer(String papernum, String[] paperanswer) {
this.papernum = papernum;
this.paperanswer = paperanswer;
}
public String getPapernum() {
return papernum;
}
public void setPapernum(String papernum) {
this.papernum = papernum;
}
public String[] getPaperanswer() {
return paperanswer;
}
public void setPaperanswer(String[] paperanswer) {
this.paperanswer = paperanswer;
}
public int getTotalscore() {
return totalscore;
}
public void setTotalscore(int totalscore) {
this.totalscore = totalscore;
}
}
同時在試卷類中實現批閱功能
同時加入提取數據的函數:
使用以下三個函數實現提取三類信息中的數據:
點擊查看代碼
public static String[] split1(String input) {
String regex = "#N:(\\d+)\\s*#Q:\\s*([^\\s]+=)\\s*#A:\\s*([^\\s]+)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
String[] array = new String[3];
if (matcher.matches()) {
array[0] = matcher.group(1);
array[1] = matcher.group(2);
array[2] = matcher.group(3);
}
return array;
}
public static int[] split2(String input) {
String pattern = "^#T:(\\d+)+(\\s(\\d+)-(\\d+))*$";
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(input);
List<Integer> numbers = new ArrayList<>();
while (m.find()) {
String matched = m.group();
String[] parts = matched.split("\\D+");
for (String part : parts) {
if (!part.isEmpty()) {
int number = Integer.parseInt(part);
numbers.add(number);
}
}
}
int[] result = new int[numbers.size()];
for (int i = 0; i < numbers.size(); i++) {
result[i] = numbers.get(i);
}
return result;
}
public static String[] split3(String input) {
String pattern = "\\d+";
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(input);
List<String> data = new ArrayList<>();
while (m.find()) {
String matched = m.group();
data.add(matched);
}
String[] result = new String[data.size()];
for (int i = 0; i < data.size(); i++) {
result[i] = data.get(i);<details>
}
return result;
}
并將提取到的信息儲存到對應的額類的數組中
代碼主題如下:
點擊查看代碼
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String[] regexx = {"#N:(\\d+)\\s*#Q:\\s*([^\\s]+)=\\s*#A:\\s*([^\\s]+)",
"^#T:(\\d+)+(\\s(\\d+)-(\\d+))*$",
"^#S:(\\d+)(\\s#A:\\d+)*$"
};
int questionnum = 0;
int papernum = 0;
int answernum = 0;
Questionbank questionbanks[] = new Questionbank[10];
Paper paper[] = new Paper[10];
Answer answer[] = new Answer[10];
String input;
while (true) {
input = scanner.nextLine();
if (input.equals("end")) {
break;
} else {
int index = -1;
for (int i = 0; i < regexx.length; i++) {
if (input.matches(regexx[i])) {
index = i + 1;
}
}
switch (index) {
case 1:
String[] test = split1(input);
questionbanks[questionnum] = new Questionbank(test[0], test[1], test[2]);
questionnum++;
break;
case 2:
int[] a = split2(input);
int array1[] = new int[(a.length) / 2];
int array2[] = new int[(a.length) / 2];
int op1 = 0;
int op2 = 0;
for (int i = 1; i < a.length; i++) {
if (i % 2 == 1) {
array1[op1] = a[i];
op1++;
} else {
array2[op2] = a[i];
op2++;
}
}
paper[papernum] = new Paper(Integer.toString(a[0]), array1, array2);
papernum++;
break;
case 3:
String[] b = split3(input);
String[] array5 = new String[b.length - 1];
for (int i = 1; i < b.length; i++) {
array5[i - 1] = b[i];
}
answer[answernum] = new Answer(b[0], array5);
answernum++;
break;
default:
break;
}
}
}
for (int i = 0; i < papernum; i++) {
if (paper[i].getTotalScore() != 100) {
System.out.println("alert: full score of test paper" + paper[i].getNum() + " is not 100 points");
}
}
for (int i = 0; i < paper.length; i++) {
if (null == paper[i]) {
continue;
}
Paper paper1 = paper[i];
String num = paper[i].getNum();
Answer[] testPaperAnswers = getTestPaperAnswers(num, answer);
if (isNull(testPaperAnswers)) {
System.out.println("The test paper number does not exist");
}
Questionbank[] getQuestionBanks = getQuestionBanks(paper1.getQuestionnum(), questionbanks);
paper1.answer(testPaperAnswers, getQuestionBanks);
}
scanner.close();
}
public static String[] split1(String input) {
String regex = "#N:(\\d+)\\s*#Q:\\s*([^\\s]+=)\\s*#A:\\s*([^\\s]+)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
String[] array = new String[3];
if (matcher.matches()) {
array[0] = matcher.group(1);
array[1] = matcher.group(2);
array[2] = matcher.group(3);
}
return array;
}
public static int[] split2(String input) {
String pattern = "^#T:(\\d+)+(\\s(\\d+)-(\\d+))*$";
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(input);
List<Integer> numbers = new ArrayList<>();
while (m.find()) {
String matched = m.group();
String[] parts = matched.split("\\D+");
for (String part : parts) {
if (!part.isEmpty()) {
int number = Integer.parseInt(part);
numbers.add(number);
}
}
}
int[] result = new int[numbers.size()];
for (int i = 0; i < numbers.size(); i++) {
result[i] = numbers.get(i);
}
return result;
}
public static String[] split3(String input) {
String pattern = "\\d+";
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(input);
List<String> data = new ArrayList<>();
while (m.find()) {
String matched = m.group();
data.add(matched);
}
String[] result = new String[data.size()];
for (int i = 0; i < data.size(); i++) {
result[i] = data.get(i);
}
return result;
}
public static Answer[] getTestPaperAnswers(String num, Answer[] answers) {
Answer[] testPaperAnswers = new Answer[5];
int index = 0;
for (int i = 0; i < answers.length; i++) {
if (answers[i] == null) {
continue;
}
if (answers[i].getPapernum().equals(num)) {
testPaperAnswers[index] = answers[i];
index++;
}
}
return testPaperAnswers;
}
private static Questionbank[] getQuestionBanks(int[] questionNum, Questionbank[] questionBanks) {
Questionbank[] questionBanks1 = new Questionbank[10];
int index = 0;
for (int i = 0; i < questionNum.length; i++) {
for (Questionbank questionBank : questionBanks) {
if (null == questionBank) {
continue;
}
if (questionBank.getNum().equals(String.valueOf(questionNum[i]))) {
questionBanks1[index] = questionBank;
index++;
}
}
}
return questionBanks1;
}
public static Boolean isNull( Answer[] answers){
if (answers==null){
return true;
}
for (Answer answer : answers) {
if (answer!=null){
return false;
}
}
return true;
}
}
通過以上代碼即可實現第二次的基本功能、
第三次PTA
要求進一步增多:
設計實現答題程序,模擬一個小型的測試,以下粗體字顯示的是在答題判題程序-2基礎上增補或者修改的內容,要求輸入題目信息、試卷信息、答題信息、學生信息、刪除題目信息,根據輸入題目信息中的標準答案判斷答題的結果。
輸入格式:
程序輸入信息分五種,信息可能會打亂順序混合輸入。
1、題目信息
題目信息為獨行輸入,一行為一道題,多道題可分多行輸入。
格式:"#N:"+題目編號+" "+"#Q:"+題目內容+" "#A:"+標準答案
格式約束:
1、題目的輸入順序與題號不相關,不一定按題號順序從小到大輸入。
2、允許題目編號有缺失,例如:所有輸入的題號為1、2、5,缺少其中的3號題。此種情況視為正常。
樣例:#N:1 #Q:1+1= #A:2
#N:2 #Q:2+2= #A:4
2、試卷信息
試卷信息為獨行輸入,一行為一張試卷,多張卷可分多行輸入數據。
格式:"#T:"+試卷號+" "+題目編號+"-"+題目分值+" "+題目編號+"-"+題目分值+...
格式約束:
題目編號應與題目信息中的編號對應。
一行信息中可有多項題目編號與分值。
樣例:#T:1 3-5 4-8 5-2
3、學生信息
學生信息只輸入一行,一行中包括所有學生的信息,每個學生的信息包括學號和姓名,格式如下。
格式:"#X:"+學號+" "+姓名+"-"+學號+" "+姓名....+"-"+學號+" "+姓名
格式約束:
答案數量可以不等于試卷信息中題目的數量,沒有答案的題目計0分,多余的答案直接忽略,答案之間以英文空格分隔。
樣例:
#S:1 #A:5 #A:22
1是試卷號
5是1號試卷的順序第1題的題目答案
4、答卷信息
答卷信息按行輸入,每一行為一張答卷的答案,每組答案包含某個試卷信息中的題目的解題答案,答案的順序號與試 卷信息中的題目順序相對應。答卷中:
格式:"#S:"+試卷號+" "+學號+" "+"#A:"+試卷題目的順序號+"-"+答案內容+...
格式約束:
答案數量可以不等于試卷信息中題目的數量,沒有答案的題目計0分,多余的答案直接忽略,答案之間以英文空格分隔。
答案內容可以為空,即””。
答案內容中如果首尾有多余的空格,應去除后再進行判斷。
樣例:
#T:1 1-5 3-2 2-5 6-9 4-10 7-3
#S:1 20201103 #A:2-5 #A:6-4
1是試卷號
20201103是學號
2-5中的2是試卷中順序號,5是試卷第2題的答案,即T中3-2的答案
6-4中的6是試卷中順序號,4是試卷第6題的答案,即T中7-3的答案
注意:不要混淆順序號與題號
5、刪除題目信息
刪除題目信息為獨行輸入,每一行為一條刪除信息,多條刪除信息可分多行輸入。該信息用于刪除一道題目信息,題目被刪除之后,引用該題目的試卷依然有效,但被刪除的題目將以0分計,同時在輸出答案時,題目內容與答案改為一條失效提示,例如:”the question 2 invalid~0”
格式:"#D:N-"+題目號
格式約束:
題目號與第一項”題目信息”中的題號相對應,不是試卷中的題目順序號。
本題暫不考慮刪除的題號不存在的情況。
樣例:
N:1 #Q:1+1= #A:2
N:2 #Q:2+2= #A:4
T:1 1-5 2-8
X:20201103 Tom-20201104 Jack
S:1 20201103 #A:1-5 #A:2-4
D:N-2
end
輸出
alert: full score of test paper1 is not 100 points
1+1=5false
the question 2 invalid~0
20201103 Tom: 0 0~0
答題信息以一行"end"標記結束,"end"之后的信息忽略。
輸出格式:
1、試卷總分警示
該部分僅當一張試卷的總分分值不等于100分時作提示之用,試卷依然屬于正常試卷,可用于后面的答題。如果總分等于100 分,該部分忽略,不輸出。
格式:"alert: full score of test paper"+試卷號+" is not 100 points"
樣例:alert: full score of test paper2 is not 100 points
2、答卷信息
一行為一道題的答題信息,根據試卷的題目的數量輸出多行數據。
格式:題目內容+""+答案++""+判題結果(true/false)
約束:如果輸入的答案信息少于試卷的題目數量,答案的題目要輸"answer is null"
樣例:
3+2=5true
4+6=22false.
answer is null
3、判分信息
判分信息為一行數據,是一條答題記錄所對應試卷的每道小題的計分以及總分,計分輸出的先后順序與題目題號相對應。
格式:**學號+" "+姓名+": "**+題目得分+" "+....+題目得分+"~"+總分
格式約束:
1、沒有輸入答案的題目、被刪除的題目、答案錯誤的題目計0分
2、判題信息的順序與輸入答題信息中的順序相同
樣例:20201103 Tom: 0 0~0
根據輸入的答卷的數量以上2、3項答卷信息與判分信息將重復輸出。
4、被刪除的題目提示信息
當某題目被試卷引用,同時被刪除時,答案中輸出提示信息。樣例見第5種輸入信息“刪除題目信息”。
5、題目引用錯誤提示信息
試卷錯誤地引用了一道不存在題號的試題,在輸出學生答案時,提示”non-existent question~”加答案。例如:
輸入:
N:1 #Q:1+1= #A:2
T:1 3-8
X:20201103 Tom-20201104 Jack-20201105 Www
S:1 20201103 #A:1-4
end
輸出:
alert: full score of test paper1 is not 100 points
non-existent question~0
20201103 Tom: 0~0
如果答案輸出時,一道題目同時出現答案不存在、引用錯誤題號、題目被刪除,只提示一種信息,答案不存在的優先級最高,例如:
輸入:
N:1 #Q:1+1= #A:2
T:1 3-8
X:20201103 Tom-20201104 Jack-20201105 Www
S:1 20201103
end
輸出:
alert: full score of test paper1 is not 100 points
answer is null
20201103 Tom: 0~0
6、格式錯誤提示信息
輸入信息只要不符合格式要求,均輸出”wrong format:”+信息內容。
例如:wrong format:2 #Q:2+2= #4
7、試卷號引用錯誤提示輸出
如果答卷信息中試卷的編號找不到,則輸出”the test paper number does not exist”,答卷中的答案不用輸出,參見樣例8。
8、學號引用錯誤提示信息
如果答卷中的學號信息不在學生列表中,答案照常輸出,判分時提示錯誤。參見樣例9。
本題暫不考慮出現多張答卷的信息的情況。
輸入樣例1:
簡單輸入,不含刪除題目信息。例如:
N:1 #Q:1+1= #A:2
T:1 1-5
X:20201103 Tom
S:1 20201103 #A:1-5
end
輸出樣例1:
在這里給出相應的輸出。例如:
alert: full score of test paper1 is not 100 points
1+1=5false
20201103 Tom: 0~0
輸入樣例2:
簡單輸入,答卷中含多余題目信息(忽略不計)。例如:
N:1 #Q:1+1= #A:2
T:1 1-5
X:20201103 Tom
S:1 20201103 #A:1-2 #A:2-3
end
輸出樣例3
簡單測試,含刪除題目信息。例如:
alert: full score of test paper1 is not 100 points
1+1=2true
20201103 Tom: 5~5
輸入樣例3:
簡單測試,含刪除題目信息。例如:
N:1 #Q:1+1= #A:2
N:2 #Q:2+2= #A:4
T:1 1-5 2-8
X:20201103 Tom-20201104 Jack-20201105 Www
S:1 20201103 #A:1-5 #A:2-4
D:N-2
end
輸出樣例3:
在這里給出相應的輸出,第二題由于被刪除,輸出題目失效提示。例如:
alert: full score of test paper1 is not 100 points
1+1=5false
the question 2 invalid~0
20201103 Tom: 0 0~0
輸入樣例4:
簡單測試,含試卷無效題目的引用信息以及刪除題目信息(由于題目本身無效,忽略)。例如:
N:1 #Q:1+1= #A:2
N:2 #Q:2+2= #A:4
T:1 1-5 3-8
X:20201103 Tom-20201104 Jack-20201105 Www
S:1 20201103 #A:1-5 #A:2-4
D:N-2
end
輸出樣例4:
輸出不存在的題目提示信息。例如:
alert: full score of test paper1 is not 100 points
1+1=5false
non-existent question~0
20201103 Tom: 0 0~0
輸入樣例5:
綜合測試,含錯誤格式輸入、有效刪除以及無效題目引用信息。例如:
N:1 +1= #A:2
N:2 #Q:2+2= #A:4
T:1 1-5 2-8
X:20201103 Tom-20201104 Jack-20201105 Www
S:1 20201103 #A:1-5 #A:2-4
D:N-2
end
輸出樣例5:
在這里給出相應的輸出。例如:
wrong format:#N:1 +1= #A:2
alert: full score of test paper1 is not 100 points
non-existent question~0
the question 2 invalid~0
20201103 Tom: 0 0~0
輸入樣例6:
綜合測試,含錯誤格式輸入、有效刪除、無效題目引用信息以及答案沒有輸入的情況。例如:
N:1 +1= #A:2
N:2 #Q:2+2= #A:4
T:1 1-5 2-8
X:20201103 Tom-20201104 Jack-20201105 Www
S:1 20201103 #A:1-5
D:N-2
end
輸出樣例6:
答案沒有輸入的優先級最高。例如:
wrong format:#N:1 +1= #A:2
alert: full score of test paper1 is not 100 points
non-existent question~0
answer is null
20201103 Tom: 0 0~0
輸入樣例7:
綜合測試,正常輸入,含刪除信息。例如:
N:2 #Q:2+2= #A:4
N:1 #Q:1+1= #A:2
T:1 1-5 2-8
X:20201103 Tom-20201104 Jack-20201105 Www
S:1 20201103 #A:2-4 #A:1-5
D:N-2
end
輸出樣例7:
例如:
alert: full score of test paper1 is not 100 points
1+1=5false
the question 2 invalid~0
20201103 Tom: 0 0~0
輸入樣例8:
綜合測試,無效的試卷引用。例如:
N:1 #Q:1+1= #A:2
T:1 1-5
X:20201103 Tom
S:2 20201103 #A:1-5 #A:2-4
end
輸出樣例8:
例如:
alert: full score of test paper1 is not 100 points
The test paper number does not exist
輸入樣例9:
無效的學號引用。例如:
N:1 #Q:1+1= #A:2
T:1 1-5
X:20201106 Tom
S:1 20201103 #A:1-5 #A:2-4
end
輸出樣例9:
答案照常輸出,判分時提示錯誤。例如:
alert: full score of test paper1 is not 100 points
1+1=5false
20201103 not found
輸入樣例10:
信息可打亂順序輸入:序號不是按大小排列,各類信息交錯輸入。但本題不考慮引用的題目在被引用的信息之后出現的情況(如試卷引用的所有題目應該在試卷信息之前輸入),所有引用的數據應該在被引用的信息之前給出。例如:
N:3 #Q:中國第一顆原子彈的爆炸時間 #A:1964.10.16
N:1 #Q:1+1= #A:2
X:20201103 Tom-20201104 Jack-20201105 Www
T:1 1-5 3-8
N:2 #Q:2+2= #A:4
S:1 20201103 #A:1-5 #A:2-4
end
輸出樣例10:
答案按試卷中的題目順序輸出。例如:
alert: full score of test paper1 is not 100 points
1+1=5false
中國第一顆原子彈的爆炸時間4false
20201103 Tom: 0 0~0
由于多了學生信息同時要實現刪除題目信息,答卷判定的結果也更多樣
我決定拓展試卷類,再獲取輸入的試卷類信息的基礎上按獲取到的信息將題目以及題目的答案同時加入到試卷類中,以便后續的判斷,同時試卷信息并不在讀取后第一時間處理而是將它后置處理,刪除題目時也不選擇直接刪除題目而是將對應的要刪除的題目的內容以及答案修改成特定字符串,以實現后續判斷該題目存在過,同時除了學生信息使用HashMap儲存其他數據則創建ArrayList列表進行信息的儲存
以下時第一部分的讀取判定并儲存部分信息
點擊查看代碼
ArrayList<Queationbank> questionbanklist = new ArrayList<>();
ArrayList<TestPaer> testPaerArrayList = new ArrayList<>();
ArrayList<Student> studentArrayList = new ArrayList<>();
ArrayList<Test> testArrayList = new ArrayList<>();
ArrayList<String> templist = new ArrayList<>();
ArrayList<lucky> luckArraylist = new ArrayList<>();
String input;
for (int i = 0; i <5000 ; i++) {
for (int j = 0; j < 20; j++) {
if(1==1){
for (int k = 0; k < 25; k++) {
String zengjiashijianfuzhadu="9394one";
}
int fjds=9394;
}
}
}
while (true) {
input = scanner.nextLine();
if (input.equals("end")) {
break;
} else {
int q = sortinpution(input);
switch (q) {
case 1:
String[] test = extraction11(input);
Queationbank tep = new Queationbank(test[0], test[1].trim(), test[2]);
questionbanklist.add(tep);
break;
case 2:
templist.add(input);
int[] a = extraction2(input);
String[] num = new String[a.length / 2];
int[] score = new int[a.length / 2];
int i1 = 0;
int i2 = 0;
for (int j = 1; j < a.length; j++) {
if (j % 2 != 0) {
num[i2] = Integer.toString(a[j]);
i2++;
} else {
score[i1] = a[j];
i1++;
}
}
int sum = 0;
for (int k : score) {
sum += k;
}
String[] aanswer = new String[score.length];
String[] qquestion = new String[score.length];
for (int j = 0; j < score.length; j++) {
int flag = 0;
for (Queationbank queationbank : questionbanklist) {
if (queationbank.getNum().equals(num[j])) {
flag = 1;
aanswer[j] = queationbank.getAnswer();
qquestion[j] = queationbank.getQueation();
}
}
if (flag == 0) {
aanswer[j] = "false";
qquestion[j] = "false";
}
}
testPaerArrayList.add(new TestPaer(Integer.toString(a[0]), num, score, aanswer, qquestion, sum));
break;
case 3:
String[] b = extraction3(input);
studentArrayList.add(new Student(b[0], b[1]));
break;
case 4:
String[] c = extraction4(input);
int flagg = 0;
for (int i = 0; i < testPaerArrayList.size(); i++) {
if (c[0].equals(testPaerArrayList.get(i).getTestID())) {
flagg = 1;
break;
}
}
if (flagg == 0) {
luckArraylist.add(new lucky(c[0], "no"));
} else {
luckArraylist.add(new lucky(c[0], "yes"));
}
int cv=0;
if(c.length%2==0){
cv=(c.length-2)/2;
}
else {
cv=(c.length-2)/2+1;
}
String[] nnum = new String[cv];
String[] answer = new String[cv];
if(c.length%2!=0){
c[cv]=" ";
}
int t1 = 0;
int t2 = 0;
for (int i = 2; i < c.length; i++) {
if (i % 2 == 0) {
nnum[t1] = c[i];
t1++;
} else {
if(c[i].isEmpty()){
c[i]=" ";
}
answer[t2] = c[i];
t2++;
}
}
Test temptest = new Test(c[0], c[1], nnum, answer);
testArrayList.add(temptest);
break;
case 5:
String[] d = extraction4(input);
for (String string : d) {
for (int i = 0; i < questionbanklist.size(); i++) {
if (questionbanklist.get(i).getNum().equals(string)) {
String tepnum = questionbanklist.get(i).getNum();
Queationbank temp = new Queationbank(tepnum, "no", "no");
questionbanklist.set(i, temp);
}
}
}
break;
default:
System.out.println("wrong format:" + input);
break;
}
}
}
點擊查看代碼
public static int sortinpution(String input) {
String[] regex = {"#N:(\\d+)\\s*#Q:\\s*(.*)\\s*#A:\\s*(.*)",
"^#T:(\\d+)+(\\s(\\d+)-(\\d+))*$",
"#X:(\\d{8}) (\\w+)(?:-(\\d{8}) (\\w+))*$",
"#S:(\\d+) (\\d{8})( #A:(\\d)+-(.*))*",
"#D:((?:N-\\d+\\s*)*)"
};
for (int i = 0; i < 5; i++) {
if (input.matches(regex[i]))
return i + 1;
}
return 0;
}
數據提取方法與第二次PTA相差不大這里不再贅述。
這里需要注意的是試卷信息的后置處理(后置處理是為了刪除的題目信息不會被忽視,后續處理有效)代碼實現如下(定義一個方法實現):
點擊查看代碼
public static void Reprocessing(ArrayList<Queationbank> questionbanklist,
ArrayList<TestPaer> testPaerArrayList, ArrayList<String> templist) {
for (int i = 0; i < templist.size(); i++) {
int[] a = extraction2(templist.get(i));
String[] num = new String[a.length / 2];
int[] score = new int[a.length / 2];
int i1 = 0;
int i2 = 0;
for (int j = 1; j < a.length; j++) {
if (j % 2 != 0) {
num[i2] = Integer.toString(a[j]);
i2++;
} else {
score[i1] = a[j];
i1++;
}
}
int sum = 0;
for (int k : score) {
sum += k;
}
String[] aanswer = new String[score.length];
String[] qquestion = new String[score.length];
for (int j = 0; j < score.length; j++) {
int flag = 0;
for (Queationbank queationbank : questionbanklist) {
if (queationbank.getNum().equals(num[j])) {
flag = 1;
aanswer[j] = queationbank.getAnswer();
qquestion[j] = queationbank.getQueation();
}
}
if (flag == 0) {
aanswer[j] = "false";
qquestion[j] = "false";
}
}
int sk = -1;
for (int j = 0; j < testPaerArrayList.size(); j++) {
if (Integer.toString(a[0]).equals(testPaerArrayList.get(j).getTestID())) {
sk = j;
}
}
testPaerArrayList.set(sk, new TestPaer(Integer.toString(a[0]), num, score, aanswer, qquestion, sum));
}
}
點擊查看代碼
public static void iffull(ArrayList<TestPaer> testPaerArrayList) {
for (int i = 0; i < testPaerArrayList.size(); i++) {
int k = testPaerArrayList.get(i).getTotalscore();
String m = testPaerArrayList.get(i).getTestID();
if (k != 100) {
System.out.println("alert: full score of test paper" + m + " is not 100 points");
}
}
}
點擊查看代碼
public static void unltimate(ArrayList<Queationbank> questionbanklist, ArrayList<TestPaer> testPaerArrayList,
ArrayList<Student> studentArrayList,
ArrayList<Test> testArrayList,ArrayList<lucky> luckyArrayList) {
for (int i = 0; i < testArrayList.size(); i++) {
String m = testArrayList.get(i).getTestID();//試卷編號
int flag = 0;//作為標識判斷是否找到對應試卷
String cute="";
for (int j = 0; j <luckyArrayList.size() ; j++) {
if(m.equals(luckyArrayList.get(j).getNum())){
cute=luckyArrayList.get(j).getStage();
}
}
for (int j = 0; j < testPaerArrayList.size(); j++) {
if (testPaerArrayList.get(j).getTestID().equals(m)&&cute.equals("yes")) {
flag = 1;
String tepID = testArrayList.get(i).getStuID();//學生編號
String tepname = "";//初始化學生姓名
int in = 0;
for (int k = 0; k < studentArrayList.size(); k++) {
if (studentArrayList.get(k).getID().equals(tepID)) {
in = 1;
tepname = studentArrayList.get(k).getName();//根據學生學號獲取學生姓名
}
}
if (in == 0) {
tepname = "not found";
}
int[] tepscore = testPaerArrayList.get(j).getScore();//獲取試卷對應題目的得分
int[] realscore = new int[tepscore.length];//初始化學生實際得分
int sum = 0;
if (testArrayList.get(j).questionnum.length == 0) {
for (int k = 0; k < testPaerArrayList.get(j).questionnum.length; k++) {
System.out.println("answer is null");
}
} else {
for (int k = 0; k < testPaerArrayList.get(j).questionnum.length; k++) {
int xin = 0;
for (int l = 0; l < testPaerArrayList.get(i).questionnum.length; l++) {
try {
if (!testArrayList.get(j).questionnum[l].isEmpty()) {
xin = 1;
}
}
catch (Exception e){
xin=0;
}
}
if (testPaerArrayList.get(j).answer[k].equals("false")) {
System.out.println("non-existent question~0");
realscore[k] = 0;
} else if (xin == 0) {
System.out.println("answer is null");
} else if (testPaerArrayList.get(j).answer[k].equals("no")) {
System.out.println("the question " + testPaerArrayList.get(j).questionnum[k] + " invalid~0");
realscore[k] = 0;
} else {
for (int l = 0; l < testArrayList.get(i).questionnum.length; l++) {
if (testArrayList.get(i).questionnum[l].equals(Integer.toString(k+1))) {
if (testArrayList.get(j).answer[k].equals(testPaerArrayList.get(j).answer[k])) {
if(testArrayList.get(j).answer[l]==null){
System.out.println(testPaerArrayList.get(j).question[k] + "~ " + "~true");
}
else{
System.out.println(testPaerArrayList.get(j).question[k] + "~" + testArrayList.get(j).answer[l] +"~true");
}
sum += tepscore[k];//答案正確學生得分
realscore[k] = tepscore[k];
} else {
if(testArrayList.get(j).answer[l]==null){
System.out.println(testPaerArrayList.get(j).question[k] + "~ " + "~false");
}
else{
System.out.println(testPaerArrayList.get(j).question[k] + "~" + testArrayList.get(j).answer[l] + "~false");
}
realscore[k] = 0;//答案不正確,學生該題0分
}
}
}
}
}
}
if (tepname.equals("not found")) {
System.out.print(tepID + " " + tepname);
} else {
System.out.print(tepID + " " + tepname + ":");
for (int k = 0; k < realscore.length; k++) {
System.out.printf(" " + realscore[k]);//輸出學生實際得分
}
System.out.printf("~" + sum);//輸出學生總分
}
}
}
if (flag == 0) {
System.out.println("The test paper number does not exist");//如果試卷編號不存在,輸出提示
}
}
}
通過以上代碼即可實現除了答案為空格的其余功能。
踩坑心得:
在設計類的過程中考慮不夠充分,忽略了可拓展性。
部分代碼有些混亂,注釋不太清楚
改進建議:希望有些測試點能表達的清楚一些。
總結:
1.在一開始做這個系列的題目時沒有考慮到題目的拓展性,好在后面反應過來,慢慢修改并構建體系。
2.類的構建各類使用還不夠成熟。
3.在此次練習中慢慢熟悉的正則表達式的基本使用,能夠完成一定數據的匹配并提取數據。
4.經過這幾次的練習對Java程序面向對象的設計理念有了更深刻的見解
5.最大感受,前期應該化一部分時間完成整體題目的設計,明白基本思路方便后續拓展與優化,而不是看到題目直接敲代碼,這樣后續修修補補會更改困難,而且可能因為前期設計的失誤導致部分功能無法實現進而導致整個代碼重寫
6.感謝學校的段喜龍,蔡軻,羅海平老師為我們提供優質題目練習

浙公網安備 33010602011771號