首页 > 其他分享 >OOP课程·PTA题目集1-3总结

OOP课程·PTA题目集1-3总结

时间:2024-04-20 17:33:22浏览次数:14  
标签:题目 String int 样例 PTA OOP return public

一. 前言

  1. 第一次pta比较简单,主要考察了类的关联与依赖,只有一道大题与4个小题
  2. 第二次pta比较难,主要考察了类设计,有一道大题与3个小题
  3. 第三次pta较难,主要考察了类设计,日期类的基本使用,有2个小题与1个大题

二.设计与分析
第一次题目集

  • 7-1 设计一个风扇Fan类
    源码:
点击查看代码
import java.util.Scanner;
class Fan{
    private final int SLOW = 1;
    private final int MEDIUM = 2;
    private final int FAST = 3;
    private int speed;
    private boolean on;
    private double radius;
    private String color;
    public Fan(){
        this.speed = SLOW;
        this.on = false;
        this.radius = 5;
        this.color = "white";
    }
    public Fan(int speed, boolean on, double radius, String color) {
        this.speed = speed;
        this.on = on;
        this.radius = radius;
        this.color = color;
    }
    public int getSpeed() {
        return this.speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public boolean isOn() {
        return this.on;
    }

    public void setOn(boolean on) {
        this.on = on;
    }

    public double getRadius() {
        return this.radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }

    public String color() {
        return this.color;
    }

    public void setColor(String color) {
        this.color = color;
    }
    public String toString() {
        if (on) {
            return "speed " + this.speed +"\n"+ "color " + this.color +"\n"+ "radius " + this.radius+"\n"+"fan is on\n";
        }
        else {
            return "speed " + this.speed +"\n"+ "color " + this.color +"\n"+ "radius " + this.radius+"\n"+"fan is off\n";
        }
    }
}
public class Main{
    public static void main(String[]args){
        Scanner input=new Scanner(System.in);
        Fan fan1=new Fan();
        Fan fan2=new Fan(input.nextInt(),input.nextBoolean(),input.nextDouble(),input.next());
        System.out.printf("-------\nDefault\n-------\n");
        System.out.print(fan1.toString());
        System.out.printf("-------\nMy Fan\n-------\n");
        System.out.print(fan2.toString());
    }

类图:

分析:按照题意设计风扇类,对输入输出进行处理即可

  • 7-2 类和对象的使用
    源码:
点击查看代码
import java.util.Scanner;
class Student{
    private String name;
    private String sex;
    private String studentID;
    private int age;
    private String major;
    public Student(){
    }
    public Student(String name,String sex,int age,String major,String studentID){
        this.name=name;
        this.sex=sex;
        this.studentID=studentID;
        this.age=age;
        this.major=major;
    }
   public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getStudentID() {
        return studentID;
    }
    public void setStudentID(String studentID) {
        this.studentID = studentID;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getMajor() {
        return major;
    }
    public void setMajor(String major) {
        this.major = major;
    }
    public String toString(){  
        return "姓名:"+this.name+",性别:"+this.sex+",学号:" + this.studentID+",年龄:"+this.age+",专业:"+this.major;
    }
    public void printlnfo(){
        System.out.printf("%s",toString());
    }
}
public class Main{
    public static void main(String[]args){
        Scanner input=new Scanner(System.in);
        Student student=new Student(input.next(),input.next(),input.nextInt(),input.next(),input.next());
        student.printlnfo();
    }
}

类图:

分析:按题意设计学生类,创建学生对象并初始化,再输出学生信息

  • 7-3 成绩计算-1-类、数组的基本运用
    源码:
点击查看代码
import java.util.Scanner;
import java.text.DecimalFormat;
import java.math.BigDecimal;
class Student{
    private String studentID;
    private String name;
    private int chineseGrade;
    private int mathGrade;
    private int physicsGrade;
    public Student(){
        
    }
    public Student(String studentID,String name,int chineseGrade,int mathGrade,int physicsGrade){
        this.studentID=studentID;
        this.name=name;
        this.chineseGrade=chineseGrade;
        this.mathGrade=mathGrade;
        this.physicsGrade=physicsGrade;
    }
    public int sum(){
        return this.chineseGrade+this.mathGrade+this.physicsGrade;
    }
    public double average(){
        double f=(double)this.sum()/3.00;
        return f;
    }
    public String toString(){
        double f=this.average();
        DecimalFormat df = new DecimalFormat("#.00");
        return this.studentID+" "+this.name+" "+this.sum()+" "+df.format(f)+"\n";
    }
}
public class Main{
    public static void main(String[]args){
        Scanner input =new Scanner(System.in);
        Student student1=new Student(input.next(),input.next(),input.nextInt(),input.nextInt(),input.nextInt());
        Student student2=new Student(input.next(),input.next(),input.nextInt(),input.nextInt(),input.nextInt());
        Student student3=new Student(input.next(),input.next(),input.nextInt(),input.nextInt(),input.nextInt());
        Student student4=new Student(input.next(),input.next(),input.nextInt(),input.nextInt(),input.nextInt());
        Student student5=new Student(input.next(),input.next(),input.nextInt(),input.nextInt(),input.nextInt());
        System.out.print(student1.toString());
        System.out.print(student2.toString());
        System.out.print(student3.toString());
        System.out.print(student4.toString());
        System.out.print(student5.toString());
    }
}

类图:

分析:按照题意设计学生类,初始化对象并输出成绩。

  • 7-4成绩计算-2-关联类
    源码:
点击查看代码
import java.util.Scanner;
import java.text.DecimalFormat;
import java.math.BigDecimal;
class Grade{
    private int usualGrade;
    private int finalGrade;
    public Grade(){
    }
    public Grade(int usualGrade,int finalGrade){
        this.usualGrade=usualGrade;
        this.finalGrade=finalGrade;
    }
    public int totalGrade(){
        return (int)(this.usualGrade*0.4+this.finalGrade*0.6);
    } 
    public int getUsualGrade(){
        return this.usualGrade;
    }
    public int getFinalGrade(){
        return this.finalGrade;
    }
}
class Student{
    private String studentID;
    private String name;
    private Grade chineseGrade;
    private Grade mathGrade;
    private Grade physicsGrade;
    public Student(){
        
    }
    public Student(String studentID,String name,Grade chineseGrade,Grade mathGrade,Grade physicsGrade){
        this.studentID=studentID;
        this.name=name;
        this.chineseGrade=chineseGrade;
        this.mathGrade=mathGrade;
        this.physicsGrade=physicsGrade;
    }
    public int sumTotal(){
        return this.chineseGrade.totalGrade()+this.mathGrade.totalGrade()+this.physicsGrade.totalGrade();
    }
    public int sumUsual(){
        return this.chineseGrade.getUsualGrade()+this.mathGrade.getUsualGrade()+this.physicsGrade.getUsualGrade();
    }
    public int sumFinal(){
        return this.chineseGrade.getFinalGrade()+this.mathGrade.getFinalGrade()+this.physicsGrade.getFinalGrade();
    }
    public double averageTotal(){
        double f=(double)this.sumTotal()/3.00;
        return f;
    }
    public double averageUsual(){
        double f=(double)this.sumUsual()/3.00;
        return f;
    }
    public double averageFinal(){
        double f=(double)this.sumFinal()/3.00;
        return f;
    }
    public String toString(){
        double f1=this.averageTotal();
        double f2=this.averageUsual();
        double f3=this.averageFinal();
        DecimalFormat df = new DecimalFormat("#.00");
        
        return this.studentID+" "+this.name+" "+this.sumTotal()+" "+df.format(f2)+" "+df.format(f3)+" "+df.format(f1)+"\n";
    }
}
public class Main{
    public static void main(String[]args){
        Scanner input=new Scanner(System.in);
        Student []stu=new Student[3];
        for(int i=0;i<3;i++){
            Grade chineseGrade=new Grade();
            Grade mathGrade=new Grade();
            Grade physicsGrade=new Grade();
            String studentID11=input.next();
            String name11=input.next();
            String subject1=input.next();
            switch(subject1){
                case "语文":chineseGrade=new Grade(input.nextInt(),input.nextInt());break;
                case "数学":mathGrade=new Grade(input.nextInt(),input.nextInt());break;
                case "物理":physicsGrade=new Grade(input.nextInt(),input.nextInt());break;
            }
            String studentID12=input.next();
            String name12=input.next();
            String subject2=input.next();
            switch(subject2){
                case "语文":chineseGrade=new Grade(input.nextInt(),input.nextInt());break;
                case "数学":mathGrade=new Grade(input.nextInt(),input.nextInt());break;
                case "物理":physicsGrade=new Grade(input.nextInt(),input.nextInt());break;
            }
            String studentID13=input.next();
            String name13=input.next();
            String subject3=input.next();
            switch(subject3){
                case "语文":chineseGrade=new Grade(input.nextInt(),input.nextInt());break;
                case "数学":mathGrade=new Grade(input.nextInt(),input.nextInt());break;
                case "物理":physicsGrade=new Grade(input.nextInt(),input.nextInt());break;
            }
            stu[i]=new Student(studentID11,name11,chineseGrade,mathGrade,physicsGrade);
            System.out.print(stu[i].toString());
        }
    }
}

类图:

分析:运用了类的关联,把成绩类作为学生类的属性,处理输入,再输出

  • 7-5答题判题程序-1
    设计实现答题程序,模拟一个小型的测试,要求输入题目信息和答题信息,根据输入题目信息中的标准答案判断答题的结果。
    输入格式:
    程序输入信息分三部分:

1、题目数量
格式:整数数值,若超过1位最高位不能为0,
样例:34

2、题目内容
一行为一道题,可以输入多行数据。
格式:"#N:"+题号+" "+"#Q:"+题目内容+" "#A:"+标准答案
格式约束:题目的输入顺序与题号不相关,不一定按题号顺序从小到大输入。
样例:#N:1 #Q:1+1= #A:2
#N:2 #Q:2+2= #A:4
3、答题信息
答题信息按行输入,每一行为一组答案,每组答案包含第2部分所有题目的解题答案,答案的顺序号与题目题号相对应。
格式:"#A:"+答案内容
格式约束:答案数量与第2部分题目的数量相同,答案之间以英文空格分隔。
样例:#A:2 #A:78
2是题号为1的题目的答案
78是题号为2的题目的答案
答题信息以一行"end"标记结束,"end"之后的信息忽略。
输出格式:
1、题目数量
格式:整数数值,若超过1位最高位不能为0,
样例:34
2、答题信息
一行为一道题的答题信息,根据题目的数量输出多行数据。
格式:题目内容+" ~"+答案
样例:1+1=~2
2+2= ~4
3、判题信息
判题信息为一行数据,一条答题记录每个答案的判断结果,答案的先后顺序与题目题号相对应。
格式:判题结果+" "+判题结果
格式约束:
1、判题结果输出只能是true或者false,
2、判题信息的顺序与输入答题信息中的顺序相同
样例:true false true
输入样例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。

源码:

点击查看代码
import java.util.*;
import java.lang.*;
class Question{
    private int number;
    private String content;
    private String standardAnswer;
    public Question(int number, String content, String standardAnswer) {
        this.number = number;
        this.content = content;
        this.standardAnswer = standardAnswer;
    }
    public void setNumber(int number){
        this.number=number;
    }
    public void setContent(String content) {
       this.content = content;
    }
    public void setStandardAnswer(String standardAnswer) {
        this.standardAnswer = standardAnswer;
    }
    public int getNumber() {
        return number;
    }
    public String getContent() {
        return content;
    }
    public String getStandardAnswer() {
        return standardAnswer;
    }
    
    public boolean checkAnswer(String answer) {
        return answer.equals(standardAnswer);
    }
}

class ExaminationPaper{
    private List<Question>questionList;
    private int questionCount;
    public ExaminationPaper(int questionCount) {
        this.questionList = new ArrayList<>(questionCount);
        this.questionCount = questionCount;
    }
    public void addQuestion(int n,Question question) {
        questionList.add(n,question);
    }
    public boolean checkAnswer(int num,String answer){
        Question question = questionList.get(num);
        return question.checkAnswer(answer);
    }
    public Question question(int num){
        return questionList.get(num);
    }
}

class Answer{
    private ExaminationPaper test;
    private List<String>answers;
    private List<Boolean>results;
    public Answer(ExaminationPaper test,int questionCount){
        this.test=test;
        answers = new ArrayList<>(questionCount);
        results = new ArrayList<>(questionCount);
    }
    public void addAnswer(int n,String answer){
        answers.add(n,answer);
    }
    public void addResults(int n){
        results.add(n,this.test.checkAnswer(n,answers.get(n)));
    }
    
    public void printResult(int n){
        for(int i=0;i<n;i++){
            String p=((this.test.question(i)).getContent()).substring(3);
            String q=p.trim();
            System.out.println(q+"~"+(answers.get(i)).substring(3));
        }
        for(int i=0;i<n;i++){
            System.out.print(results.get(i));
            if(i!=n-1)
                System.out.print(" ");
        }
    }
}
public class Main{
    public static void main(String [] args){
        Scanner input =new Scanner(System.in);
        int n=input.nextInt();
        input.nextLine();
        Question []question=new Question[n];
        ExaminationPaper test=new ExaminationPaper(100);       
        for(int i=0;i<n;i++){
            String line = input.nextLine();
            String line2=line.trim();
            String[] parts = line2.split("\\s+");
            String []parts2=new String[3];
            if(parts.length>3){
                int index1=0;
                int index2=0;
                int index3=0;
                for(int i1=0;i1<parts.length;i1++) {
                    if(parts[i1].contains("#N")){
                        index1=i1;
                    }
                    if(parts[i1].contains("#Q")){
                        index2=i1;
                    }
                    if(parts[i1].contains("#A")){
                        index3=i1;
                    }
                }
                parts2[0]=parts[index1];
                parts2[1]=parts[index2].trim();
                parts2[2]=parts[index3];
                for(int j=index1+1;j<index2;j++){
                    parts2[0]+=parts[j];
                }
                for(int j=index2+1;j<index3;j++){
                    parts2[1]+=" "+parts[j];
                }
                for(int j=index3+1;j<parts.length;j++){
                    parts2[2]+=parts[j];
                }
            }
            else{
                parts2[0]=parts[0];
                parts2[1]=parts[1];
                parts2[2]=parts[2];
            }
            int number = Integer.parseInt(parts2[0].substring(3));
            question[number-1]=new Question(number,parts2[1],parts2[2]);
        }
        for(int i=0;i<n;i++){
            test.addQuestion(i,question[i]);
        }
        Answer answers=new Answer(test,n);
        for(int i=0;i<n;i++){
            String ans=input.next();
            answers.addAnswer(i,ans);
        }
        for(int i=0;i<n;i++){
            answers.addResults(i);
        }
        answers.printResult(n);
    }
}

类图:

分析:设计3个类,问题类,试卷类,答卷类。先对输入字符串进行处理拆分,保存到问题中,把题目加到试卷中,把试卷加到答卷中,然后对答案字符串拆分,保存到答卷中,最后进行判题并输出。

第二次题目集

  • 7-1 手机按价格排序、查找
    源码:
点击查看代码
import java.util.*;
class MobilePhone implements Comparable<MobilePhone> {
    private String type;
    private int price;
    public MobilePhone(){
    }
    public MobilePhone(String type,int price){
        this.type=type;
        this.price=price;
    }
    public int getPrice(){
        return this.price;
    }
    public String getType(){
        return this.type;
    }
    public String toString(){
        return "型号:"+this.type+",价格:"+this.price;
    }
    @Override
    public int compareTo(MobilePhone otherMobilePhone) {
        return this.price - otherMobilePhone.price;
    }
}
public class Main{
    public static void main(String[]args){
        Scanner input =new Scanner(System.in);
        LinkedList<MobilePhone> MobilePhoneList = new LinkedList<>();
        MobilePhoneList.add(new MobilePhone(input.next(),input.nextInt()));
        MobilePhoneList.add(new MobilePhone(input.next(),input.nextInt()));
        MobilePhoneList.add(new MobilePhone(input.next(),input.nextInt()));
        
        System.out.println("排序前,链表中的数据:");
        for(int i=0;i<3;i++){
            System.out.printf("%s\n",(MobilePhoneList.get(i)).toString());
        }
        Collections.sort(MobilePhoneList);
        
        System.out.println("排序后,链表中的数据:");
        for(int i=0;i<3;i++){
            System.out.printf("%s\n",(MobilePhoneList.get(i)).toString());
        }
        
        MobilePhone goal=new MobilePhone(input.next(),input.nextInt());
        int flag=0;
        for(int i=0;i<3;i++){
            if(goal.getPrice()==(MobilePhoneList.get(i)).getPrice()){
                System.out.println(goal.getType()+"与链表中的"+(MobilePhoneList.get(i)).getType()+"价格相同");
                flag=1;
                //break;
            }
        }
        if(flag==0){
            System.out.print("链表中的对象,没有一个与"+goal.getType()+"价格相同的");
        }
    }
}

类图:

分析:设计手机类,了解相关知识可实现Comparable接口,重写compareTo方法,完成排序

  • 7-2sdut-oop-4-求圆的面积(类与对象)

源码:

点击查看代码
import java.util.*;
class Circle{
    private int radius;
    public Circle(){
        this.radius=2;
        System.out.println("This is a constructor with no para.");
    }
    public Circle(int radius){
        if(radius>0)
            this.radius=radius;
        else
            this.radius=2;
        System.out.println("This is a constructor with para.");
    }
    public void setRadius(int radius){
        if(radius>0)
            this.radius=radius;
        else
            this.radius=2;
    }
    public int getRadius(){
        return this.radius;
    }
    public double getArea(){
        return this.radius*this.radius*Math.PI;
    }
    public String toString( ){
        return "Circle [radius=" + this.radius + "]";
    }
}
public class Main{
    public static void main(String[]args){
        Scanner input =new Scanner(System.in);
	    Circle c1=new Circle();
        System.out.printf("%s\n",c1.toString());
        System.out.printf("%.2f\n",c1.getArea());
        
        Circle c2=new Circle();
        System.out.printf("%s\n",c2.toString());
        System.out.printf("%.2f\n",c2.getArea());
        
        c1.setRadius(input.nextInt());
        System.out.printf("%s\n",c1.toString());
        System.out.printf("%.2f\n",c1.getArea());
        
        c2=new Circle(input.nextInt());
        System.out.printf("%s\n",c2.toString());
        System.out.printf("%.2f\n",c2.getArea());
        
    }
}

类图:

分析:该题很简单,了解类和对象的相关知识就能做出了

  • 7-3 Java类与对象-汽车类
    源码:
点击查看代码
import java.util.*;

/**
 * @author Teacher
 *         主类,用来在pta中运行。
 *         其中有一处代码需要完善
 */
public class Main {
  public static void main(String[] args) {

    // 此处填写合适的代码【1】
    // 生成汽车实例
    float a=0;
    Car car=new Car("bob",a,a);
      
    System.out.printf("车主:%s,速度:%.1f,角度:%.1f\n", car.getOwnerName(), car.getCurSpeed(), car.getCurDirInDegree());

    // 设置新速度
    car.changeSpeed(10);
    System.out.printf("车主:%s,速度:%.1f,角度:%.1f\n", car.getOwnerName(), car.getCurSpeed(), car.getCurDirInDegree());

    // 停车
    car.stop();
    System.out.printf("车主:%s,速度:%.1f,角度:%.1f\n", car.getOwnerName(), car.getCurSpeed(), car.getCurDirInDegree());

  }
}

/**
 * @author Teacher
 *         汽车类,其中有两个内容需要完善。
 */
class Car {
  // 车主姓名
  private String ownerName;
  // 当前车速
  private float curSpeed;
  // 当前方向盘转向角度
  private float curDirInDegree;

  public Car(String ownerName) {
    this.ownerName = ownerName;
  }

  public Car(String ownerName, float speed, float dirInDegree) {
    this(ownerName);
    this.curSpeed = speed;
    this.curDirInDegree = dirInDegree;
  }
  // 提供对车主姓名的访问
  public String getOwnerName() {
    return ownerName;
  }
  // 提供对当前方向盘转向角度的访问
  public float getCurDirInDegree() {
    return curDirInDegree;
  }
  // 提供对当前车速的访问
  public float getCurSpeed() {
    return curSpeed;
  }

  // 提供改变当前的车速
  public void changeSpeed(float curSpeed) {
    // 此处填写合适的代码【2】
    this.curSpeed=curSpeed;
  }

  // 提供停车
  public void stop() {
    // 此处填写合适的代码【3】
      this.curSpeed=0;
      this.curDirInDegree=0;
  }
}

类图:

分析:该题很简单,了解类和对象的相关知识就能做出了。

  • 7-4答题判题程序-2

设计实现答题程序,模拟一个小型的测试,以下粗体字显示的是在答题判题程序-1基础上增补或者修改的内容。
要求输入题目信息、试卷信息和答题信息,根据输入题目信息中的标准答案判断答题的结果。
输入格式:
程序输入信息分三种,三种信息可能会打乱顺序混合输入:
1、题目信息
一行为一道题,可输入多行数据(多道题)。
格式:"#N:"+题目编号+" "+"#Q:"+题目内容+" "#A:"+标准答案

格式约束:

1、题目的输入顺序与题号不相关,不一定按题号顺序从小到大输入。
2、允许题目编号有缺失,例如:所有输入的题号为1、2、5,缺少其中的3号题。此种情况视为正常。
样例:#N:1 #Q:1+1= #A:2
#N:2 #Q:2+2= #A:4
2、试卷信息
一行为一张试卷,可输入多行数据(多张卷)。
格式:"#T:"+试卷号+" "+题目编号+"-"+题目分值
题目编号应与题目信息中的编号对应。
一行信息中可有多项题目编号与分值。
样例:#T:1 3-5 4-8 5-2
3、答卷信息
答卷信息按行输入,每一行为一张答卷的答案,每组答案包含某个试卷信息中的题目的解题答案,答案的顺序与试卷信息中的题目顺序相对应。
格式:"#S:"+试卷号+" "+"#A:"+答案内容
格式约束:答案数量可以不等于试卷信息中题目的数量,没有答案的题目计0分,多余的答案直接忽略,答案之间以英文空格分隔。
样例:#S:1 #A:5 #A:22
1是试卷号
5是1号试卷的顺序第1题的题目答案
22是1号试卷的顺序第2题的题目答案
答题信息以一行"end"标记结束,"end"之后的信息忽略。
输出格式:
1、试卷总分警示
该部分仅当一张试卷的总分分值不等于100分时作提示之用,试卷依然属于正常试卷,可用于后面的答题。如果总分等于100分,该部分忽略,不输出。
格式:"alert: full score of test paper"+试卷号+" is not 100 points"
样例:alert: full score of test paper2 is not 100 points
2、答卷信息
一行为一道题的答题信息,根据试卷的题目的数量输出多行数据。
格式:题目内容+""+答案++""+判题结果(true/false)

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

样例:3+2=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

源码:

点击查看代码
import java.util.*;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;

class Question{
    private int number;
    private String content;
    private String standardAnswer;
    public Question(){
    	
    }
    
    public Question(int number, String content, String standardAnswer) {
        this.number = number;
        this.content = content;
        this.standardAnswer = standardAnswer;
    }
    
    public void setNumber(int number){
        this.number=number;
    }
    
    public void setContent(String content) {
       this.content = content;
    }
    
    public void setStandardAnswer(String standardAnswer) {
        this.standardAnswer = standardAnswer;
    }
    
    public int getNumber() {
        return number;
    }
    
    public String getContent() {
        return content;
    }
    
    public String getStandardAnswer() {
        return standardAnswer;
    }
   
    public boolean checkAnswer(String answer) {
        return answer.equals(standardAnswer);
    }
}

class ExaminationPaper{
    private int number;
    private List<Question>questionList;
    private List<Integer>scorelist= new ArrayList<>();
    private int questionCount;
    public ExaminationPaper(){
        this.questionList= new ArrayList<>();
    }
    public ExaminationPaper(int questionCount) {
        this.questionList = new ArrayList<>(questionCount);
        this.setQuestionCount(questionCount);
    }
    public void addScore(int n,int score){
        this.scorelist.add(n,score);
    }
    public int getScore(int n){
        return this.scorelist.get(n);
    }
    public void setNumber(int number){
        this.number=number;
    }
    public int getNumber(){
        return this.number;
    }
    public void addQuestion(int n,Question question) {
        questionList.add(n,question);
    }
    public boolean checkAnswer(int num,String answer){
        Question question = questionList.get(num);
        return question.checkAnswer(answer);
    }
    public Question question(int num){
        return questionList.get(num);
    }
	public int getQuestionCount() {
		return questionCount;
	}
	public void setQuestionCount(int questionCount) {
		this.questionCount = questionCount;
	}
    public int sumOfQuestions(){
        int sum=0;
        for(int i=0;i<questionCount;i++){
            sum+=scorelist.get(i);
        }
        return sum;
    }
    public void scoreWarning(){
        if(this.sumOfQuestions()!=100)
            System.out.println("alert: full score of test paper"+this.number+" is not 100 points"); 
    }
}

class Answer{
    private ExaminationPaper test;
    private List<String>answers=new ArrayList<>();;
    private List<Boolean>results=new ArrayList<>();;
    private int number;
    private int num;
    private boolean paperIsExist;
    public Answer() {
    	this.paperIsExist=false;
    }
    public int getTestQuestionCount(){
        return this.test.getQuestionCount();
    }
    public boolean getPaperIsExist(){
        return this.paperIsExist;
    }
    public void PaperIsExist(){
        if(this.test==null)
            this.paperIsExist=false;
        else
            this.paperIsExist=true;
    }
    
    public Answer(ExaminationPaper test,int questionCount){
        this.test=test;
        this.answers = new ArrayList<>(questionCount);
        this.results = new ArrayList<>(questionCount);
    }
    public void setResultSize(int size){
        this.results = new ArrayList<>(size);
    }
    public void setTest(ExaminationPaper test){
        this.test=test;
    }
    public void setNum(int num){
        this.num=num;
    }
    public int getNum(){
        return this.num;
    }
    public void setNumber(int number){
        this.number=number;
    }
    public int getNumber(){
        return this.number;
    }
    public void addAnswer(int n,String answer){
    	this.answers.add(n,answer);
    }
    public String getAnswer(int n){
        return this.answers.get(n);
    }
    public void addResults(int n){
    	this.results.add(n,this.test.checkAnswer(n,answers.get(n)));
    }
    public void questionscore(int n){
        int sum=0;
        for(int i=0;i<n;i++){
            if(this.results.get(i)){
                sum+=this.test.getScore(i);
                System.out.printf("%d",this.test.getScore(i));
                if(i!=n-1){
                    System.out.print(" ");
                }
            }
            else{
                System.out.print("0");
                if(i!=n-1){
                    System.out.print(" ");
                }
            }
        }
        if(this.test.getQuestionCount()>n){
            for(int i=0;i<this.test.getQuestionCount()-n;i++){
                System.out.print(" "+"0");
            }
        }
        System.out.print("~"+sum+"\n");
    }
    public void printResult(int n){
        for(int i=0;i<n;i++){
            System.out.printf("%s~%s~%s\n",this.test.question(i).getContent(),this.answers.get(i),this.results.get(i));
        }
        if(this.test.getQuestionCount()>n){
            for(int i=0;i<this.test.getQuestionCount()-n;i++){
                System.out.println("answer is null");
            }
        }
        this.questionscore(n);
    }
}

public class Main{
    public static void main(String [] args){
        Scanner input =new Scanner(System.in);
        String []line=new String[200];
        List<String>str1=new ArrayList<String>();
        List<String>str2=new ArrayList<String>();
        List<String>str3=new ArrayList<String>();
        Pattern pattern1 = Pattern.compile("#N:.*#Q:.*#A:");
        Pattern pattern2 = Pattern.compile("#T:.*");
        Pattern pattern3 = Pattern.compile("#S:.*#A:.*");
        int i=0;
        int numOfQuestion=0;
        int numOfExaminationPaper=0;
        int numOfAnswer=0;
        while(input.hasNextLine()){
            line[i]=input.nextLine();
            if(line[i].equals("end")){
                break;
            }
            Matcher matcher1 = pattern1.matcher(line[i]);
            Matcher matcher2 = pattern2.matcher(line[i]);
            Matcher matcher3 = pattern3.matcher(line[i]);
            if(matcher1.find()){
                numOfQuestion++;
                str1.add(line[i]);
            }
            else if(matcher2.find()){
                numOfExaminationPaper++;
                str2.add(line[i]);
            }
            else if(matcher3.find()){
                numOfAnswer++;
                str3.add(line[i]);
            }
            i++;
        }
        
        int maxOfQuestion=0;
        for(int j=0;j<numOfQuestion;j++){
            int number=0;
            Pattern pattern = Pattern.compile("#N:\\s*(\\d+)\\s*#Q:\\s*(.*?)\\s*#A:\\s*(.*)\\s*");
            Matcher matcher = pattern.matcher(str1.get(j));
            if(matcher.find()){
                number = Integer.parseInt(matcher.group(1).trim());
            }
            if(maxOfQuestion<number)
                maxOfQuestion=number;
        }
        Question []questions=new Question[maxOfQuestion];
        for(int j=0;j<maxOfQuestion;j++){
             questions[j]=new Question();
        }
        for(int j=0;j<numOfQuestion;j++){
            int number=0;
            String content;
            String standardAnswer;
            Pattern pattern = Pattern.compile("#N:\\s*(\\d+)\\s*#Q:\\s*(.*?)\\s*#A:\\s*(.*)\\s*");
            Matcher matcher = pattern.matcher(str1.get(j));
            if(matcher.find()){
                number = Integer.parseInt(matcher.group(1).trim());
                content= matcher.group(2).trim();
                standardAnswer = matcher.group(3).trim();
                int c=number-1;
                questions[c]=new Question(number,content,standardAnswer);
            }
        }
        ExaminationPaper []test=new ExaminationPaper[numOfExaminationPaper];
        for(int j=0;j<numOfExaminationPaper;j++){
            int count=0;
            test[j]=new ExaminationPaper();
            Pattern pattern = Pattern.compile("#T:\\s*(\\d+)\\s*(.*)");
            Matcher matcher = pattern.matcher(str2.get(j));
            if(matcher.find()){
                int number = Integer.parseInt(matcher.group(1).trim());
                test[j].setNumber(number);
                String text=matcher.group(2).trim();
                String []parts1=text.split("\\s+");
                int []numberOfQuestion2=new int [parts1.length];
                int []scoresOfQuestion=new int [parts1.length];
                for(int ii=0;ii<parts1.length;ii++){
                    String []parts2=parts1[ii].split("-");
                    numberOfQuestion2[ii]=Integer.parseInt(parts2[0]);                        
                    scoresOfQuestion[ii]=Integer.parseInt(parts2[1]);
                    count++;
                }
                
                for(int k=0;k<parts1.length;k++){
                    test[j].addQuestion(k,questions[numberOfQuestion2[k]-1]);
                    test[j].addScore(k,scoresOfQuestion[k]);
                }   
                
            }
            test[j].setQuestionCount(count);
        }  

        
        Answer []answer=new Answer[numOfAnswer];
        for(int j=0;j<numOfAnswer;j++){
            answer[j]=new Answer();
        }
        for(int j=0;j<numOfAnswer;j++){
            int number=0;
            String text;
            Pattern pattern = Pattern.compile("#S:\\s*(\\d+)\\s*(.*)");
            Matcher matcher = pattern.matcher(str3.get(j));
            
            if(matcher.find()){
                int size=0;
                number = Integer.parseInt(matcher.group(1).trim());
                for(int k=0;k<numOfExaminationPaper;k++){
                    if(number==test[k].getNumber()){
                        answer[j].setTest(test[k]);
                        answer[j].PaperIsExist();
                        break;
                    }
                }
                answer[j].setNumber(number);
                text=matcher.group(2).trim();
                String text1=text.replaceAll("#A:","");
                String []answers=text1.split("\\s+");
                if(answer[j].getPaperIsExist()){
                    size=answer[j].getTestQuestionCount();
                    for(int k=0;k<(size<answers.length?size:answers.length);k++){
                        answer[j].addAnswer(k,answers[k]);
                    }
                }
                answer[j].setNum((size<answers.length?size:answers.length));
            }
        }
        for(int j=0;j<numOfAnswer;j++){
            if(answer[j].getPaperIsExist()){
                answer[j].setResultSize(answer[j].getNum());
                for(int k=0;k<answer[j].getNum();k++){
                    answer[j].addResults(k);
                }
            }
            
        }
        for(int j=0;j<numOfExaminationPaper;j++){
            test[j].scoreWarning();
        }
        for(int j=0;j<numOfAnswer;j++){
            if(answer[j].getPaperIsExist()){
                answer[j].printResult(answer[j].getNum());
            }
            else{
                System.out.println("The test paper number does not exist");
            }
        }
    }
}

类图:

分析:先用正则表达式对输入的每行字符串进行分类,分为题目,试卷,答卷三种信息。再依次对题目试卷答卷类字符串进行拆分,设置到题目,试卷,答卷中。再对答案进行校验,最后先输出分数提示,再输出答卷及判分信息。

第三次题目集

  • 7-1面向对象编程(封装性)
    源码:
点击查看代码

import java.util.*;

class Student{
    private String sid;
    private String name;
    private int age;
    private String major;

    public Student(){
        
    }
    public Student(String sid,String name,int age,String major){
        this.setSid(sid);
        this.setName(name);
        this.setAge(age);
        this.setMajor(major);
    }
	public String getID() {
		return sid;
	}
	public void setSid(String sid) {
		this.sid = sid;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		if(age>=0){
            this.age=age;
        }
	}
	public String getMajor() {
		return major;
	}
	public void setMajor(String major) {
		this.major = major;
	}
    public void print(){
        System.out.println("学号:"+sid+",姓名:"+name+",年龄:"+age+",专业:"+major);
    }
}
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //调用无参构造方法,并通过setter方法进行设值
        String sid1 = sc.next();
        String name1 = sc.next();
        int age1 = sc.nextInt();
        String major1 = sc.next();
        Student student1 = new Student();
        student1.setSid(sid1);
        student1.setName(name1);
        student1.setAge(age1);
        student1.setMajor(major1);
        //调用有参构造方法
        String sid2 = sc.next();
        String name2 = sc.next();
        int age2 = sc.nextInt();
        String major2 = sc.next();
        Student student2 = new Student(sid2, name2, age2, major2);
        //对学生student1和学生student2进行输出
        student1.print();
        student2.print();
    }
}

/* 请在这里填写答案 */

类图:

分析:该题很简单,了解类和对象的相关知识就能做出了。

  • 7-2 jmu-java-日期类的基本使用
    源码:
点击查看代码
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        String dateStr1 = input.nextLine();
        String dateStr2 = input.nextLine();
        if (!isValidDate(dateStr1)) {
            System.out.println(dateStr1 + "无效!");
        } 
        else { 
            Date date1 = parseDate(dateStr1);
            Calendar calendar1 = Calendar.getInstance();
            calendar1.setTime(date1);
            int year = calendar1.get(Calendar.YEAR);
            boolean isLeapYear = isLeapYear(year);
            if(isLeapYear){
                System.out.println(dateStr1 +"是闰年.");
            }
            int week=calendar1.get(Calendar.DAY_OF_WEEK);
            if(week==1){
                 week=7;
            }
            else{
                week-=1;
            }   
            System.out.println(dateStr1 + "是当年第" + calendar1.get(Calendar.DAY_OF_YEAR) + "天,当月第" + calendar1.get(Calendar.DAY_OF_MONTH) + "天,当周第" + week + "天.");
        }

        
        String[] dates = dateStr2.split(" ");
        boolean isValid = true;
        for (int i=0;i<=1;i++) {
            if (!isValidDate(dates[i])) {
                isValid = false;
                break;
            }
        }
        if(isValid==false){
            System.out.println(dates[0]+"或" +dates[1]+ "中有不合法的日期.");
        }
        
        if (isValid && compareDates(dates[0], dates[1]) <= 0) {
            Date startDate = parseDate(dates[0]);
            Date endDate = parseDate(dates[1]);
            long diffDays = getDifferenceInDays(startDate, endDate);
            int diffMonths = getDifferenceInMonths(startDate,endDate);
            int diffYears = getDifferenceInYears(startDate, endDate);
            System.out.printf("%s与%s之间相差%d天,所在月份相差%d,所在年份相差%d.",dates[1],dates[0],diffDays,diffMonths,diffYears);
        }
        if (isValid){
            warnning(dates[0],dates[1]);
        }
        
    }
    public static boolean isValidDate(String dateStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Pattern pattern = Pattern.compile("\\d{4}-\\d{2}-\\d{2}");
        Matcher matcher = pattern.matcher(dateStr);
        if(matcher.find()&&dateStr.length()==10){
            sdf.setLenient(false);
            try {
                sdf.parse(dateStr);
                return true;
            } catch (ParseException e) {
                return false;
            }
        }
        else{
            return false;
        }
        
    }

    
    public static Date parseDate(String dateStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return sdf.parse(dateStr);
        } catch (ParseException e) {
            return null;
        }
    }

    
    public static boolean isLeapYear(int year) {
        return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
    }

   
    public static int compareDates(String dateStr1, String dateStr2) {
        Date date1 = parseDate(dateStr1);
        Date date2 = parseDate(dateStr2);
        return date1.compareTo(date2);
    }

  
    public static long getDifferenceInDays(Date startDate, Date endDate) {
        long diff = endDate.getTime() - startDate.getTime();
        return diff / (24 * 60 * 60 * 1000);
    }

    
    public static int getDifferenceInMonths(Date startDate, Date endDate) {
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(startDate);
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(endDate);

        int diffYears = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR);
        int diffMonths = endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH);
        return /*diffYears * 12+*/  diffMonths;
    }
    public static int getDifferenceInYears(Date startDate, Date endDate) {
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(startDate);
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(endDate);
        return endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR);
    }
    public static void warnning(String data1,String data2){
        Date startDate = parseDate(data1);
        Date endDate = parseDate(data2);
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(startDate);
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(endDate);
        if(endCalendar.before(startCalendar)){
            System.out.print(data2+"早于"+data1+",不合法!");
        }
    }
}

分析:主要考察了日期类中相关方法的使用

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

输入格式:

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

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

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

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

2、试卷信息

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

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

3、学生信息

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

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

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

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

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

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

5、删除题目信息

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

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

格式约束:

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

   本题暂不考虑删除的题号不存在的情况。      

样例:

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

输出

alert: full score of test paper1 is not 100 points
1+1=~5~false
the question 2 invalid~0
20201103 Tom: 0 0~0
 答题信息以一行"end"标记结束,"end"之后的信息忽略。

输出格式:

1、试卷总分警示

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

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

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

2、答卷信息

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

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

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

    3+2=~5~true
     4+6=~22~false.
     answer is null

3、判分信息
判分信息为一行数据,是一条答题记录所对应试卷的每道小题的计分以及总分,计分输出的先后顺序与题目题号相对应。
格式:学号+" "+姓名+": "+题目得分+" "+....+题目得分+"~"+总分
格式约束:

1、没有输入答案的题目、被删除的题目、答案错误的题目计0分
  2、判题信息的顺序与输入答题信息中的顺序相同
 样例:20201103 Tom: 0 0~0
    根据输入的答卷的数量以上2、3项答卷信息与判分信息将重复输出。

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

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

输入:

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

输出:

alert: full score of test paper1 is not 100 points
non-existent question~0
20201103 Tom: 0~0

如果答案输出时,一道题目同时出现答案不存在、引用错误题号、题目被删除,只提示一种信息,答案不存在的优先级最高,例如:
输入:

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

输出:

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

6、格式错误提示信息

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

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

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

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

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

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

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

输入样例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=~5~false
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=~2~true
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=~5~false
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=~5~false
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=~5~false
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=~5~false
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=~5~false
中国第一颗原子弹的爆炸时间~4~false
20201103 Tom: 0 0~0

源码:

点击查看代码
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class Question {
	private int number=0;
	private String content;
	private String standarAnswer;
	private boolean isDelect=false;
	
	public Question() {
		
	}
	public Question(int number,String content,String standarAnswer) {
		this.number=number;
		this.content=content;
		this.standarAnswer=standarAnswer;
	}
	
	public int getNumber() {
		return this.number;
	}
	public void setNumber(int number) {
		this.number = number;
	}
	public String getContent() {
		return this.content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public String getStandarAnswer() {
		return this.standarAnswer;
	}
	public void setStandarAnswer(String standarAnswer) {
		this.standarAnswer = standarAnswer;
	}
	public boolean getDelect() {
		return this.isDelect;
	}
	public void setDelect(int number) {
		if(this.number==number) {
			this.isDelect=true;
		}
	}
	public boolean checkAnswer(String answer) {
		if(this.standarAnswer.equals(answer)) {
			return true;
		}
		else
			return false;
	}
}

class Student {
	private List<Integer>ID=new ArrayList<>();
	private List<String>name=new ArrayList<>();
	public Student() {
		
	}
	public int getID(int index) {
		return this.ID.get(index);
	}
	public void addID(int ID) {
		this.ID.add(ID);
	}
	public String getName(int index) {
		return this.name.get(index);
	}
	public void addName(String name) {
		this.name.add(name);
	}
	public int getIDSize() {
		return this.ID.size();
	}
}

class Paper {
	private int number=0;
	private List<Question>questions= new ArrayList<>();
	private List<Integer>questoinScores=new ArrayList<>();
	public Paper() {
		
	}
	public Paper(int number) {
		this.number=number;
	}
	
	public int getNumber() {
		return this.number;
	}
	public void setNumber(int number) {
		this.number = number;
	}
	public void addQuestions(Question question) {
		this.questions.add(question);
	}
	public Question getQuestions(int index) {
		return this.questions.get(index);
	}
	public void addQuestoinScores(int score) {
		this.questoinScores.add(score);
	}
	public int getQuestoinScores(int index) {
		return this.questoinScores.get(index);
	}
	public List<Integer> getQuestoinScores() {
		return questoinScores;
	}
	public List<Question>getQuestions(){
		return this.questions;
	}
	
}

class StudentAnswer {
	private int studentID;
	private List<Integer>questionsNumber=new ArrayList<>();
	private List<String>questionsAnswer=new ArrayList<>();
	public StudentAnswer() {
		
	}
	public StudentAnswer(int studentID) {
		this.setStudentID(studentID);
	}
	public int getStudentID() {
		return studentID;
	}
	public void setStudentID(int studentID) {
		this.studentID = studentID;
	}
	public void addQuestionsAnswer(String answer) {
		this.questionsAnswer.add(answer);
	}
	public String getQuestionsAnswer(int index) {
		return this.questionsAnswer.get(index);
	}
	public int getQuestionsNumber(int index) {
		return questionsNumber.get(index);
	}
	public void addQuestionsNumber(int number) {
		this.questionsNumber.add(number);
	}
	public List<String> getQuestionsAnswer() {
		return this.questionsAnswer;
	}
}

class PaperAnswer {
	private int number=0;
	private Paper test;
	private List<StudentAnswer>studentsAnswer=new ArrayList<>();
	public PaperAnswer() {
		
	}
	public PaperAnswer(int number,Paper test) {
		this.setNumber(number);
		this.setTest(test);
	}
	public int getNumber() {
		return number;
	}
	public void setNumber(int number) {
		this.number = number;
	}
	public void addStudentsAnswer(StudentAnswer answer) {
		this.studentsAnswer.add(answer);
	}
	public StudentAnswer getStudentsAnswer(int index) {
		return this.studentsAnswer.get(index);
	}
	public Paper getTest() {
		return test;
	}
	public void setTest(Paper test) {
		this.test = test;
	}
	public List<StudentAnswer> getstudentsAnswer() {
		return this.studentsAnswer;
	}
}
class View {
	public void scoreWarnning(Paper testPaper) {
		int sum=0;
		for(int i=0;i<testPaper.getQuestoinScores().size();i++) {
			sum+=testPaper.getQuestoinScores(i);
		}
		if(sum!=100) {
			System.out.println("alert: full score of test paper"+testPaper.getNumber()+" is not 100 points");
		}
	}
	public void answerInformation(PaperAnswer paperAnswer,Student students) {
		if(paperAnswer.getTest()==null) {
			System.out.println("The test paper number does not exist");
		}
		else {
			for(int i=0;i<paperAnswer.getstudentsAnswer().size();i++) {
				int n1=paperAnswer.getTest().getQuestions().size();//题目个数
				int n2=paperAnswer.getStudentsAnswer(i).getQuestionsAnswer().size();//答案个数
				int []score=new int[n1];
				if(n1<=n2) {
					for(int j=0;j<n1;j++) {
						if(paperAnswer.getTest().getQuestions(j)!=null) {
							int questionNUmber=paperAnswer.getStudentsAnswer(i).getQuestionsNumber(j);
							String str1=paperAnswer.getTest().getQuestions(j).getContent();
							String str2=paperAnswer.getStudentsAnswer(i).getQuestionsAnswer(questionNUmber-1);
							boolean result=paperAnswer.getTest().getQuestions(j).checkAnswer(str2);
							boolean isdeldect=paperAnswer.getTest().getQuestions(j).getDelect();
							if(isdeldect) {
								System.out.println("the question "+paperAnswer.getTest().getQuestions(j).getNumber()+" invalid~0");//被删除的题目提示信息
								score[j]=0;
							}
							else {
								System.out.println(str1+"~"+str2+"~"+result);
								if(result) {
									score[j]=paperAnswer.getTest().getQuestoinScores(j);
								}
								else {
									score[j]=0;
								}
							}
						}
						else {
							System.out.println("non-existent question~0");
							score[j]=0;
						}
					}
				}
				else {
                    int []arr=new int[n2];
						for(int j=0;j<n2;j++) {
							arr[j]=paperAnswer.getStudentsAnswer(i).getQuestionsNumber(j);
						}
						for(int j=0;j<n1;j++) {
                            int flag=0;
							for(int k=0;k<n2;k++) {
								if(j==(arr[k]-1)) {
                                    flag=1;
									if(paperAnswer.getTest().getQuestions(j)!=null) {
											String str1=paperAnswer.getTest().getQuestions(j).getContent();
											String str2=paperAnswer.getStudentsAnswer(i).getQuestionsAnswer(k);
											boolean result=paperAnswer.getTest().getQuestions(j).checkAnswer(str2);
											boolean isdeldect=paperAnswer.getTest().getQuestions(j).getDelect();
											if(isdeldect) {
												System.out.println("the question "+paperAnswer.getTest().getQuestions(j).getNumber()+" invalid~0");
												score[j]=0;
											}
											else {
												System.out.println(str1+"~"+str2+"~"+result);
												if(result) {
													score[j]=paperAnswer.getTest().getQuestoinScores(j);
												}
												else {
													score[j]=0;
												}
											}
									}
									else {
										System.out.println("non-existent question~0");
										score[j]=0;	
									}	
								}
							}
                            if(flag==0){
                                System.out.println("answer is null");
                            }
                            
						}
				}
				int ID=paperAnswer.getStudentsAnswer(i).getStudentID();
				int flag=0;
				int sum=0;
				for(int j=0;j<students.getIDSize();j++) {
					if(ID==students.getID(j)) {
						System.out.print(students.getID(j)+" "+students.getName(j)+": ");//学号姓名
						flag=1;
						break;
					}
				}
				if(flag==0) {
					System.out.println(ID+" not found");
				}
				else {
					for(int k=0;k<(n1);k++) {
						System.out.print(score[k]);
						sum+=score[k];
						if(k!=(n1-1)) {
							System.out.print(" ");
						}
					}
					System.out.println("~"+sum);
				}
			}
		}
	}
}


public class Main {
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		
		String []line=new String[200];
		
        List<String>str1=new ArrayList<String>();//题目信息
        List<String>str2=new ArrayList<String>();//试卷信息
        String str3=new String();//学生信息
        List<String>str4=new ArrayList<String>();//答卷信息
        List<String>str5=new ArrayList<String>();//删除题目信息
        
        Pattern pattern1 = Pattern.compile("#N:.*#Q:.*#A:.*");
        Pattern pattern2 = Pattern.compile("#T:\\d+\\s+(.*)");
        Pattern pattern3 = Pattern.compile("#X:(.*)");
        Pattern pattern4 = Pattern.compile("#S:\\d+\\s*\\d+\\s*(.*)");
        Pattern pattern5 = Pattern.compile("#D:N-\\d+");
        int i=0;
        int n1=0;//题目数量
        int n2=0;//试卷数量
        //n3=1
        int n4=0;//答卷数量
        int n5=0;//删除题目数量
        while(input.hasNextLine()){
            line[i]=input.nextLine();
            if(line[i].equals("end")){
                break;
            }
            Matcher matcher1 = pattern1.matcher(line[i]);
            Matcher matcher2 = pattern2.matcher(line[i]);
            Matcher matcher3 = pattern3.matcher(line[i]);
            Matcher matcher4 = pattern4.matcher(line[i]);
            Matcher matcher5 = pattern5.matcher(line[i]);
            if(matcher1.find()){
                if(matcher1.group().length()==line[i].length()){
                    n1++;
                    str1.add(line[i]);
                }
                else{
                    System.out.println("wrong format:"+line[i]);
                }
                
            }
            else if(matcher2.find()){
                if(matcher2.group(1).equals("")) {
                	n2++;
                    str2.add(line[i]);
                    continue;
                }
                Pattern pattern = Pattern.compile("\\d+-\\d+\\s*");
                Matcher matcher = pattern.matcher(matcher2.group(1));
                int size1=0;
                while (matcher.find()) {
                    size1+=matcher.group(0).length();
                }
                if(size1==matcher2.group(1).length()){
                    n2++;
                    str2.add(line[i]);
                }
                else{
                    System.out.println("wrong format:"+line[i]);
                }
            }
            else if(matcher3.find()){
                Pattern pattern = Pattern.compile("\\d+\\s+[a-zA-Z]*-*");
                Matcher matcher = pattern.matcher(matcher3.group(1));
                int size1=0;
                while (matcher.find()) {
                    size1+=matcher.group(0).length();
                }
                if(size1==matcher3.group(1).length()){
                    str3=new String(line[i]);
                }
                else{
                    System.out.println("wrong format:"+line[i]);
                }
                
                
            }///答案为空字符
            else if(matcher4.find()){
                Pattern pattern = Pattern.compile("#A:\\s*(\\d+)\\s*-(.*?)\\s*(?=#A:|\\n|$)");
                Matcher matcher = pattern.matcher(matcher4.group(1));
                int size1=0;
                while (matcher.find()) {
                    size1+=matcher.group(0).length();
                }
                if(size1==matcher4.group(1).length()){
                    n4++;
                    str4.add(line[i]);
                }
                else{
                    System.out.println("wrong format:"+line[i]);
                }
            }
            else if(matcher5.find()){
            	n5++;
            	str5.add(line[i]);
            }
            else {
            	System.out.println("wrong format:"+line[i]);
            }
            i++;
        }
        input.close();
        //学生信息
        Student students=new Student();
        for(int j=0;j<1;j++) {
        	Pattern pattern = Pattern.compile("#X:(.*)");
            Matcher matcher = pattern.matcher(str3);
            if(matcher.find()) {
            	String text=matcher.group(1);
            	text=text.replaceAll("-", " ");
            	text=text.trim();
            	String []IDandName=text.split("\\s+");
            	for(int k=0;k<IDandName.length;k+=2) {
            		int ID=Integer.parseInt(IDandName[k]);
            		String name=IDandName[k+1];
            		students.addID(ID);
            		students.addName(name);
            	}
            }
        }
        //题目最大编号
        int questionmaxnumber=0;
        for(int j=0;j<n1;j++) {
        	Pattern pattern = Pattern.compile("#N:\\s*(\\d+).*");
        	Matcher matcher = pattern.matcher(str1.get(j));
        	if(matcher.find()){
        		int number =Integer.parseInt(matcher.group(1).trim());
        		if(questionmaxnumber<number) {
        			questionmaxnumber=number;
        		}
        	}
        }
        //题目信息
        Question []questions=new Question[questionmaxnumber];
        for(int j=0;j<n1;j++) {
        	Pattern pattern = Pattern.compile("#N:\\s*(\\d+)\\s*#Q:\\s*(.*?)\\s*#A:\\s*(.*)\\s*");
            Matcher matcher = pattern.matcher(str1.get(j));
            if(matcher.find()){
                int number =Integer.parseInt(matcher.group(1).trim());
                String content=matcher.group(2).trim();
                String standarAnswer=matcher.group(3).trim();
                questions[number-1]=new Question(number,content,standarAnswer);
            }
        }
        //删除题目
        for(int j=0;j<n5;j++) {
        	Pattern pattern = Pattern.compile("#D:N-(\\d+)");
            Matcher matcher = pattern.matcher(str5.get(j));
            if(matcher.find()){
            	int number =Integer.parseInt(matcher.group(1).trim());
            	questions[number-1].setDelect(number);
            }
        }
        //试卷信息
        int maxPaperNumber=0;
        for(int j=0;j<n2;j++){
            Pattern pattern = Pattern.compile("#T:(\\d+)\\s+(.*)");
            Matcher matcher = pattern.matcher(str2.get(j));
            if(matcher.find()){
                int testnumber=Integer.parseInt(matcher.group(1).trim());
                if(testnumber>maxPaperNumber){
                    maxPaperNumber=testnumber;
                }
            }
        }
        Paper []testPaper=new Paper[maxPaperNumber];
        for(int j=0;j<maxPaperNumber;j++){
            testPaper[j]=new Paper(); 
        }
        for(int j=0;j<n2;j++) {
        	Pattern pattern = Pattern.compile("#T:(\\d+)\\s+(.*)");
            Matcher matcher = pattern.matcher(str2.get(j));
            if(matcher.find()){
            	int testnumber=Integer.parseInt(matcher.group(1).trim());
            	testPaper[testnumber-1].setNumber(testnumber); 
                if(matcher.group(2).equals("")){
                    continue;
                }
            	String text=matcher.group(2);
            	text=text.replaceAll("-", " ");
            	text=text.trim();
            	String []questionNumberAndScore=text.split("\\s+");
            	for(int k=0;k<questionNumberAndScore.length;k+=2) {
            		int questionNumber=Integer.parseInt(questionNumberAndScore[k].trim());
            		int score=Integer.parseInt(questionNumberAndScore[k+1].trim());
            		testPaper[testnumber-1].addQuestoinScores(score);
            		if(questionNumber>questionmaxnumber) {
            			testPaper[testnumber-1].addQuestions(null);
            		}
            		else {
            			testPaper[testnumber-1].addQuestions(questions[questionNumber-1]);
            		}            		
            	}
            }
        }
        //试卷答题信息        
        int maxNumberStudentAnswer=0;
        for(int j=0;j<n4;j++) {
        	Pattern pattern = Pattern.compile("#S:\\s*(\\d+)\\s+(\\d+)\\s*(.*)");
            Matcher matcher = pattern.matcher(str4.get(j));
            if(matcher.find()){
            	int papernumber=Integer.parseInt(matcher.group(1).trim());
            	if(papernumber>maxNumberStudentAnswer) {
            		maxNumberStudentAnswer=papernumber;
            	}
            }
        }
        PaperAnswer []paperAnswer=new PaperAnswer[maxNumberStudentAnswer];
        for(int j=0;j<maxNumberStudentAnswer;j++) {
        	paperAnswer[j]=new PaperAnswer();
        }
        int []array=new int[maxNumberStudentAnswer];
        StudentAnswer []studentAnswer=new StudentAnswer[n4];
        for(int j=0;j<n4;j++) {
        	studentAnswer[j]=new StudentAnswer();
        	Pattern pattern = Pattern.compile("#S:\\s*(\\d+)\\s+(\\d+)\\s*(.*)");
            Matcher matcher = pattern.matcher(str4.get(j));
            
            if(matcher.find()){
            	int papernumber=Integer.parseInt(matcher.group(1).trim());
                array[papernumber-1]++;
            	paperAnswer[papernumber-1].setNumber(papernumber);
            	for(int k=0;k<maxPaperNumber;k++) {
            		if(papernumber==testPaper[k].getNumber()) {
            			paperAnswer[papernumber-1].setTest(testPaper[k]);
            		}
            	}
            	
            	int ID=Integer.parseInt(matcher.group(2).trim());
            	studentAnswer[j].setStudentID(ID);
            	String text1=matcher.group(3);//对答案匹配
            	String []text2=text1.split("#A:");
            	for(int k=0;k<text2.length;k++) {
            		if(text2[k].equals("")) {
            			continue;
            		}
            		String []word=text2[k].trim().split("-");
            		int number=Integer.parseInt(word[0].trim());
                    String answer;
                    if(word.length==2){
                        answer=word[1];
                    }
            		else{
                        answer="";
                    }
            		studentAnswer[j].addQuestionsNumber(number);
            		studentAnswer[j].addQuestionsAnswer(answer);            		                       
            	}
            	paperAnswer[papernumber-1].addStudentsAnswer(studentAnswer[j]); 
            }
        }
        View view=new View();
        for(int j=0;j<maxPaperNumber;j++) {
            if(testPaper[j].getNumber()!=0){
                view.scoreWarnning(testPaper[j]);
            }
        }
        for(int j=0;j<maxNumberStudentAnswer;j++){
            if(array[j]!=0){
                view.answerInformation(paperAnswer[j], students);
            }
        }
	}   
}

类图:

分析:设计6个类,学生类,问题类,试卷类,学生答卷类,答卷类,输出类。有5种信息,先用正则表达式对每行字符串进行匹配分类,都不匹配则输出警告。再依次对字符串拆分,保存到题目试卷答卷学生中。把题目保存到试卷中,试卷保存到答卷中,答案保存到学生答卷中,学生答卷再保存到答卷中。最后进行判题并依次输出。

3.采坑心得

1.审题:看清楚题目要求,对与输入格式要仔细看,看清楚具体的格式才能更好的使用正则表达式去比配拆分等等,看懂答案输出顺序是怎样的
2.对于边界要分清楚,不然经常会非零返回
3.对于类设计结构要清晰,不然整体思路会错,在后面再改可能和重新写一样
4.对于answe is null的输出,应该是与题目的顺序一致(一开始在最后一起输出,一直没发现有错)

4.改进建议
1.对于类的职责要划分清楚,要遵守单一职责,不能全在主类中写
2.对于每一种功能的实现,应该创建一个类来履行这些职责
3.对于非零返回,应该仔细检查代码中是否有越界或读到了空
4.对类的设计应该更加具体明确

5.总结

这三次的作业总体来说较难,题目量虽然也不多,但是有些题目还是需要静下心来思考怎么去实现,要用心设计类,想好每个类应该有哪些功能。
这几次作业让我发现了自己一个问题,不会去思考哪些功能可以设计成类,喜欢把东西都放在主类中写。我感觉到没有完全掌握类的设计,应该多去思考怎么做类设计。

标签:题目,String,int,样例,PTA,OOP,return,public
From: https://www.cnblogs.com/qiuj/p/18139045

相关文章

  • 数据类型和表达式题目
    题目一求下列公式近似值题目二阿姆斯特朗数 也就是俗称的 水仙花数 ,是指一个三位数,其各位数字的立方和等于该数本身。例如:153=13+53+33,所以153就是一个 水仙花数 。求出1~1000之间所有的 水仙花数 。补充%d就是普通的输出了%2d是将数字按宽度为2,右对齐方式输出......
  • 数组题目
    数组问题1密码验证:程序设定的密码是“Y1N2ab”,从键盘输入密码(密码长度不超过10),和设定密码相比较,密码正确的话输出“that'sok”,程序结束;错误的话提示再次输入密码,最多允许输3次数组问题二编程将一个16进制的字符串转化为十进制数,如“2A”转化为42。字符串应该仅的数字和大......
  • AI极速批量换脸!Roop-unleashed下载介绍,可直播
    要说AI换脸领域,最开始火的项目就是Roop了,Roop-unleashed作为Roop的嫡系分支,不仅继承了前者的强大基因,更是在功能上实现了重大突破与升级 核心特性1、可以进行高精度的图片、视频换脸,还能实时直播换脸,换脸效果真实、自然2、不仅支持N卡处理程序(cuda),还额外提供了CPU处理模式(渣......
  • OOP题目集1~3的总结
    目录(一)前言(二)作业介绍(三)算法与代码(四)SourceMonitor代码分析(五)自学内容(六)总结一、前言介绍本篇博客的大致内容、写作目的、意义等本篇博客介绍如何使用Java语言基础和算法来解决题目问题,在此基础上进行对最近Java编程语言学习的总结题目的难度为Java入门,主要考察类......
  • 安装iptables报错
    安装ittables报错:apt-getinstalliptablesReadingpackagelists...DoneBuildingdependencytreeReadingstateinformation...DoneSomepackagescouldnotbeinstalled.Thismaymeanthatyouhaverequestedanimpossiblesituationorifyouareusingtheuns......
  • 记一次hadoop yarn环境无法提交任务的问题排查
    1.集群环境ambari-version:2.7.5HDP-version:3.02.问题描述hadoop-yarn的启动之后,运行一段时间,莫名其妙的出现新的任务无法提交上去,查看yarn的状态之后,发现yarn的状态都是正常的,并且所有的资源都是充足的,但是提交任务之后就会一直处于accept状态3.问题表现4.问题排查4.......
  • hadoop
    创建hduser:sudegeoupadhadcupaudouaeriddgridoogbdussraidocaowrehdao:n02oc0/spe/werkyyec4/xpark/ha<oop-3.3.5sudupazewdlduserhdusari-p..ual"i.zek/ii_roy.puba-/.a2h/01to0rinen.knyachmndaoo:"i.ast/autanr'aaakeyaelmodo......
  • 大数据之Hadoop(入门)
    大数据概论大数据部门业务流程分析大数据部门组织结构Hadoop生态框架Hadoop是什么Hadoop官网官网地址:http://hadoop.apache.org/releases.html下载地址:https://archive.apache.org/dist/hadoop/common/Hadoop优势Hadoop组成HDFS架构概述YARN架构概述MapRed......
  • Java题目集1-3总结
    (1)前言:第一次大作业的知识点包括类与对象正则表达式以及数组对象和不同类之间的关系;题量较小,难度不大,关键是理清question,test-paper,answer-paper之间的关系。第二次大作业的知识点增加了List,ArrayList,HashMap,三种变长数组的使用,增加了正则表达式的难度,题量增加,难度上升,试卷中的......
  • SQL题目:在数据中找出所有val全为1的key
    这道题目我觉得很经典,所以记录下来。表的结果如下:一个实体对应10行数据,所以上面的表省略了一部分以方便显示。A、B、C的元素和正文中是一样的。key为A的行val全都是NULL,key为B的行中只有i=1的行val是3,其他的都是NULL,key为C的行val全部都是1。请思考一下如何从这张表中选......