某高校课程从性质上分为:必修课、选修课,从考核方式上分为:考试、考察。
考试的总成绩由平时成绩、期末成绩分别乘以权重值得出,比如平时成绩权重0.3,期末成绩权重0.7,总成绩=平时成绩*0.3+期末成绩*0.7。
考察的总成绩直接等于期末成绩
必修课的考核方式必须为考试,选修课可以选择考试、考察任一考核方式。
1、输入:
包括课程、课程成绩两类信息。
课程信息包括:课程名称、课程性质、考核方式(可选,如果性质是必修课,考核方式可以没有)三个数据项。
课程信息格式:课程名称+英文空格+课程性质+英文空格+考核方式
课程性质输入项:必修、选修
考核方式输入选项:考试、考察
课程成绩信息包括:学号、姓名、课程名称、平时成绩(可选)、期末成绩
课程信息格式:学号+英文空格+姓名+英文空格+课程名称+英文空格+平时成绩+英文空格+期末成绩
以上信息的相关约束:
1)平时成绩和期末成绩的权重默认为0.3、0.7
2)成绩是整数,不包含小数部分,成绩的取值范围是【0,100】
3)学号由8位数字组成
4)姓名不超过10个字符
5)课程名称不超过10个字符
6)不特别输入班级信息,班级号是学号的前6位。
2、输出:
输出包含三个部分,包括学生所有课程总成绩的平均分、单门课程成绩平均分、单门课程总成绩平均分、班级所有课程总成绩平均分。
为避免误差,平均分的计算方法为累加所有符合条件的单个成绩,最后除以总数。
1)学生课程总成绩平均分按学号由低到高排序输出
格式:学号+英文空格+姓名+英文空格+总成绩平均分
如果某个学生没有任何成绩信息,输出:学号+英文空格+姓名+英文空格+"did not take any exams"
2)单门课程成绩平均分分为三个分值:平时成绩平均分(可选)、期末考试平均分、总成绩平均分,按课程名称的字符顺序输出
格式:课程名称+英文空格+平时成绩平均分+英文空格+期末考试平均分+英文空格+总成绩平均分
如果某门课程没有任何成绩信息,输出:课程名称+英文空格+"has no grades yet"
3)班级所有课程总成绩平均分按班级由低到高排序输出
格式:班级号+英文空格+总成绩平均分
如果某个班级没有任何成绩信息,输出:班级名称+英文空格+ "has no grades yet"
异常情况:
1)如果解析某个成绩信息时,课程名称不在已输入的课程列表中,输出:学号+英文空格+姓名+英文空格+":"+课程名称+英文空格+"does not exist"
2)如果解析某个成绩信息时,输入的成绩数量和课程的考核方式不匹配,输出:学号+英文空格+姓名+英文空格+": access mode mismatch"
以上两种情况如果同时出现,按第一种情况输出结果。
3)如果解析某个课程信息时,输入的课程性质和课程的考核方式不匹配,输出:课程名称+" : course type & access mode mismatch"
4)格式错误以及其他信息异常如成绩超出范围等,均按格式错误处理,输出"wrong format"
5)若出现重复的课程/成绩信息,只保留第一个课程信息,忽略后面输入的。
信息约束:
1)成绩平均分只取整数部分,小数部分丢弃
import java.text.Collator; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Scanner; public class Main { static boolean isNumber(String isNumber){ try{ int a=Integer.parseInt(isNumber); } catch (NumberFormatException e) { return false; } return true; } public static void main(String[] args) { Scanner input=new Scanner(System.in); String str=""; studentList students=new studentList(); InputMatching matching=new InputMatching(); chooseCourseList choose=new chooseCourseList(); Cls cls=new Cls(); courseList course=new courseList(); String str1,str2,str3; while(true){ str=input.nextLine(); if(isNumber(str.split(" ")[0]))break; if(str.equals("end"))break; if(InputMatching.matchingInput(str) != 1){ System.out.println("wrong format"); continue; } str1=str.split(" ")[0]; if(course.searchCourse(str1))continue; str2=str.split(" ")[1]; str3=str.split(" ")[2]; if(str2.equals("必修")&&str3.equals("考察")){ System.out.println(str.split(" ")[0]+" : course type & access mode mismatch"); continue; } if(course.searchCourse(str1))continue; course.courses.add(course.addCourse(str1,str2,str3)); } String number,name,subject; int UsualGrades,FinalGrades; boolean flag=false; while(!str.equals("end")){ if(flag)str=input.nextLine(); flag=true; if(str.equals("end"))break; if (InputMatching.matchingInput(str) != 2&&InputMatching.matchingInput(str) != 3) { System.out.println("wrong format");// 字符串符合成绩信息模式 continue; } number=str.split(" ")[0]; String Class = number.substring(0, 6); if (!cls.searchCls(Class)) cls.cls.add(cls.addCls(Class)); name=str.split(" ")[1]; students.stu.add(students.addStudent(number,name)); subject=str.split(" ")[2]; try{ if (course.searchQuality(subject) == 1) { UsualGrades = Integer.parseInt(str.split(" ")[3]); if(choose.searchCourse(subject)&&choose.searchId(number))continue; FinalGrades = Integer.parseInt(str.split(" ")[4]); students.students.add(students.addStudentCompulsory(number, name, subject, UsualGrades, FinalGrades)); } else if (course.searchQuality(subject) == 0) { UsualGrades = Integer.parseInt(str.split(" ")[3]); if(choose.searchCourse(subject)&&choose.searchId(number))continue; if(str.split(" ").length>4){System.out.println(number+" "+name+" : access mode mismatch");continue;} students.students.add(students.addStudentTake(number, name, subject, UsualGrades)); } else if (course.searchQuality(subject) == -1) { System.out.println(subject + " does not exist"); } } catch (ArrayIndexOutOfBoundsException e) { System.out.println(number+" "+name+" : access mode mismatch"); } } chooseCourseList chooses=new chooseCourseList(); chooses.getAverageGrade(students,course,cls); } } class student{ String number; String name; String course; int UsualGrades=0; int FinalGrades=0; String Class; int grade=0; public static Comparator<student> getNumberComparator() { return (s1, s2) -> s1.number.compareTo(s2.number); } } class course{ String name; String quality; String manner; public static Comparator<course> getChineseNameComparator() { final Collator collator = Collator.getInstance(java.util.Locale.CHINA); // 获取中文排序规则 return (c1, c2) -> collator.compare(c1.name, c2.name); } } class courseList{ ArrayList<course>courses=new ArrayList<>(); boolean searchCourse(String CourseName){ for (course cours : courses) if (cours.name.equals(CourseName)) return true; return false; } course addCourse( String name,String quality,String manner){ course newCourse=new course(); newCourse.name=name; newCourse.quality=quality; newCourse.manner=manner; return newCourse; } int searchQuality(String subject){ for (course cours : courses) { if (cours.name.equals(subject) && cours.quality.equals("必修")) return 1; else if (cours.name.equals(subject) && cours.manner.equals("考试")) return 1; else if (cours.name.equals(subject)) return 0; } return -1; } } class studentList { ArrayList<student> students = new ArrayList<>(); ArrayList<student> stu = new ArrayList<>(); student addStudentCompulsory(String number, String name, String course, int UsualGrades, int FinalGrades) { student newStudent = new student(); chooseCourseList newChoose = new chooseCourseList(); newStudent.name = name; if(chooseCourseList.searchCourse(course)&& chooseCourseList.chooseCourse.get(chooseCourseList.searchCourseInt(course)).name.equals(name)&& chooseCourseList.chooseCourse.get(chooseCourseList.searchCourseInt(course)).number.equals(number)) return null; Cls newCls = new Cls(); newStudent.number = number; newStudent.course = course; newStudent.grade = (int) (FinalGrades * 0.7 + UsualGrades * 0.3); String Class = number.substring(0, 6); if (!newCls.searchCls(Class)) newCls.cls.add(newCls.addCls(Class)); chooseCourseList.chooseCourse.add(newChoose.addChoose(number, name, course, Class, UsualGrades, FinalGrades, newStudent.grade)); return newStudent; } student addStudentTake(String number, String name, String course, int UsualGrades) { student newStudent = new student(); newStudent.name = name; newStudent.number = number; newStudent.course = course; newStudent.grade = UsualGrades; String Class=number.substring(0,6); chooseCourseList newChoose = new chooseCourseList(); if(chooseCourseList.searchCourse(course)&& chooseCourseList.chooseCourse.get(chooseCourseList.searchCourseInt(course)).name.equals(name)&& chooseCourseList.chooseCourse.get(chooseCourseList.searchCourseInt(course)).number.equals(number)) return null; chooseCourseList.chooseCourse.add(newChoose.addChooseTake(number, name, course, Class, UsualGrades, newStudent.grade)); return newStudent; } student addStudent(String id, String name) { student newStudent = new student(); newStudent.number = id; newStudent.name = name; return newStudent; } boolean searchStudent(String name) { for (student student : students) { if (student.name.equals(name)) return true; } return false; } } class Cls { private static int nextIndex = 0; String id; private int index; ArrayList<Cls> cls = new ArrayList<>(); public Cls addCls(String cl) { Cls newCls = new Cls(); newCls.id = cl; // 新增元素的 index 应该为当前列表长度 newCls.index = nextIndex++; cls.add(newCls); return newCls; } public boolean searchCls(String id) { for (int i = 0; i < cls.size(); i++) { if (cls.get(i).id.equals(id)) { return true; } } return false; } public static Comparator<Cls> getIndexComparator() { return new Comparator<Cls>() { @Override public int compare(Cls c1, Cls c2) { return c1.id.compareTo(c2.id); } }; } } class InputMatching { static String stuNumMatching = "[0-9]{8}";//8个0-9的数字 static String stuNameMatching = "\\S{1,10}";//1到10个非空格(TAB)字符 static String scoreMatching = "([1-9]?[0-9]|100)"; static String courseNameMatching = "\\S{1,10}";//1到10个非空格(TAB)字符 static String courseTypeMatching = "(选修|必修)"; static String checkcourseTypeMatching = "(考试|考察)"; //cousrInput用于定义课程信息模式(正则表达式) static String courseInput = courseNameMatching + " " + courseTypeMatching + " " + checkcourseTypeMatching; //scoreInput用于定义成绩信息模式(正则表达式) static String scoreInput1 = stuNumMatching + " " + stuNameMatching + " " + courseNameMatching + " " + scoreMatching + "(scoreMatching)?"; static String scoreInput2 = stuNumMatching + " " + stuNameMatching + " " + courseNameMatching + " " + scoreMatching + "(scoreMatching)?"+" "+scoreMatching + "(scoreMatching)?"; public static int matchingInput(String s) { if (matchingCourse(s)) { return 1; } if (matchingScore1(s)) { return 2; } if (matchingScore2(s)) { return 3; } return 0; } static boolean matchingCourse(String s) { return s.matches(courseInput); } static boolean matchingScore1(String s) { return s.matches(scoreInput1); } static boolean matchingScore2(String s) { return s.matches(scoreInput2); } } class chooseCourseList{ static ArrayList<student> chooseCourse=new ArrayList<>(); static boolean searchId(String id){ for (student student : chooseCourse) if (student.number.equals(id)) return true; return false; } static boolean searchCourse(String course){ for(int i=0;i<chooseCourse.size();i++) if(chooseCourse.get(i).course.equals(course))return true; return false; } static int searchCourseInt(String course){ for(int i=0;i<chooseCourse.size();i++) if(chooseCourse.get(i).course.equals(course))return i; return -1; } static boolean searchClass(String Class){ for(int i=0;i<chooseCourse.size();i++) if(chooseCourse.get(i).Class.equals(Class))return true; return false; } student addChoose(String id,String name,String course,String Class,int UsualGrades,int FinalGrades,int grade ) { student newStudent=new student(); newStudent.Class=Class; newStudent.number=id; newStudent.name=name; newStudent.course=course; newStudent.UsualGrades=UsualGrades; newStudent.FinalGrades=FinalGrades; newStudent.grade=grade; return newStudent; } student addChooseTake(String id,String name,String course,String Class,int UsualGrades,int grade ) { student newStudent=new student(); newStudent.Class=Class; newStudent.number=id; newStudent.name=name; newStudent.course=course; newStudent.UsualGrades=UsualGrades; newStudent.grade=grade; return newStudent; } public int getAverageGrades(String id){ int sum=0,cnt=0; for(int i=0;i<chooseCourse.size();i++){ if(chooseCourse.get(i).number.equals(id)){sum+=chooseCourse.get(i).grade;cnt++;} } return sum/=cnt; } public int getAverageCourseUsualGrades(String course){ int sum=0,cnt=0; for(int i=0;i<chooseCourse.size();i++){ if(chooseCourse.get(i).course.equals(course)){sum+=chooseCourse.get(i).UsualGrades;cnt++;} } return sum/=cnt; } public int getAverageCourseFinalGrades(String course){ int sum=0,cnt=0; for(int i=0;i<chooseCourse.size();i++){ if(chooseCourse.get(i).course.equals(course)){sum+=chooseCourse.get(i).FinalGrades;cnt++;} } return sum/=cnt; } public int getAverageCourseGrades(String course){ int sum=0,cnt=0; for(int i=0;i<chooseCourse.size();i++){ if(chooseCourse.get(i).course.equals(course)){sum+=chooseCourse.get(i).grade;cnt++;} } return sum/=cnt; } public int getClassAverageGrades(String Class){ int sum=0,cnt=0; for(int i=0;i<chooseCourse.size();i++){ if(chooseCourse.get(i).Class.equals(Class)){sum+=chooseCourse.get(i).grade;cnt++;} } return sum/=cnt; } public void getAverageGrade(studentList students,courseList courses,Cls cls){ Collections.sort(students.stu, student.getNumberComparator()); Collections.sort(cls.cls, Cls.getIndexComparator()); Collections.sort(courses.courses, course.getChineseNameComparator()); for(int i=0;i<students.stu.size();i++){ int flag=0; for(int j=0;j<i;j++){ if(students.stu.get(j).number.equals(students.stu.get(i).number)){flag=1;break;} } if(flag==1)continue; if(searchId(students.stu.get(i).number)){ System.out.println(students.stu.get(i).number+" "+students.stu.get(i).name+" "+getAverageGrades(students.stu.get(i).number)); } else{ System.out.println(students.stu.get(i).number+" "+students.stu.get(i).name+" did not take any exams"); } } for(int i=0;i<courses.courses.size();i++){ if(searchCourse(courses.courses.get(i).name)){ if(courses.courses.get(i).manner.equals("考试")){ System.out.println(courses.courses.get(i).name+" "+getAverageCourseUsualGrades(courses.courses.get(i).name) +" "+getAverageCourseFinalGrades(courses.courses.get(i).name)+" "+getAverageCourseGrades(courses.courses.get(i).name)); } else System.out.println(courses.courses.get(i).name+" "+getAverageCourseUsualGrades(courses.courses.get(i).name) +" "+getAverageCourseGrades(courses.courses.get(i).name)); } else{ System.out.println(courses.courses.get(i).name+" has no grades yet"); } } for(int i=0;i<cls.cls.size();i++){ int flag=0; for(int j=0;j<i;j++){ if(cls.cls.get(j).equals(cls.cls.get(i))){flag=1;break;} } if(flag==1)continue; if(searchClass(cls.cls.get(i).id)){ System.out.println(cls.cls.get(i).id+" "+getClassAverageGrades(cls.cls.get(i).id)); } else{ System.out.println(cls.cls.get(i).id+" has no grades yet"); } } } }
7-1:
输入多个学生的成绩信息,包括:学号、姓名、成绩。
学号是每个学生的唯一识别号,互不相同。
姓名可能会存在重复。
使用HashMap存储学生信息,并实现根据学号的检索功能
输入格式:
输入多个学生的成绩信息,每个学生的成绩信息格式:学号+英文空格+姓名+英文空格+成绩
以“end”为输入结束标志
end之后输入某个学号,执行程序输出该生的详细信息
输出格式:
输出查询到的学生信息格式:学号+英文空格+姓名+英文空格+成绩
如果没有查询到,则输出:"The student "+查询的学号+" does not exist"
import java.util.HashMap; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); HashMap<String , Student> hashMap = new HashMap<>(); while (true) { String id = scanner.next(); if (id.equals("end")) { break; } String name = scanner.next(); int score = scanner.nextInt(); Student student = new Student(name, score); hashMap.put(id, student); } String searchId = scanner.next(); Student student = hashMap.get(searchId); // 根据学号从 HashMap 中获取对应的学生信息 if (student != null) { System.out.println(searchId+" " + student.name + " " + student.score); } else { System.out.println("The student "+searchId+" does not exist"); } } } class Student { String name; int score; public Student(String name, int score) { this.name = name; this.score = score; } }
7-2:
输入多个学生的成绩信息,包括:学号、姓名、成绩。
学号是每个学生的唯一识别号,互不相同。
姓名可能会存在重复。
要求:使用HashMap存储学生信息。
import java.util.*; import java.util.HashMap; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); HashMap<String, Student> hashMap = new HashMap<>(); while (true) { String id = input.next(); if (id.equals("end")) { break; } String name = input.next(); int score = input.nextInt(); Student student = new Student(name, score); hashMap.put(id, student); } ArrayList<Map.Entry<String, Student>> list = new ArrayList<>(hashMap.entrySet()); hashMap.clear(); Collections.sort(list, Comparator.comparing(Map.Entry::getKey, Comparator.reverseOrder())); for(Map.Entry<String, Student> entry : list) { hashMap.put(entry.getKey(), entry.getValue()); System.out.println(entry.getKey() + " " + entry.getValue().name + " " + entry.getValue().score); } } } class Student { String name; int score; public Student(String name, int score) { this.name = name; this.score = score; } }
import java.util.*; import java.util.HashMap; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); HashMap<String, Student> hashMap = new HashMap<>(); while (true) { String id = input.next(); if (id.equals("end")) { break; } String name = input.next(); int score = input.nextInt(); Student student = new Student(name, score); hashMap.put(id, student); } ArrayList<Map.Entry<String, Student>> list = new ArrayList<>(hashMap.entrySet()); hashMap.clear(); Collections.sort(list, Comparator.comparing(Map.Entry::getKey, Comparator.reverseOrder())); for(Map.Entry<String, Student> entry : list) { hashMap.put(entry.getKey(), entry.getValue()); System.out.println(entry.getKey() + " " + entry.getValue().name + " " + entry.getValue().score); } } } class Student { String name; int score; public Student(String name, int score) { this.name = name; this.score = score; } }
7-3:
课程成绩统计程序-2在第一次的基础上增加了实验课,以下加粗字体显示为本次新增的内容。
某高校课程从性质上分为:必修课、选修课、实验课,从考核方式上分为:考试、考察、实验。
考试的总成绩由平时成绩、期末成绩分别乘以权重值得出,比如平时成绩权重0.3,期末成绩权重0.7,总成绩=平时成绩*0.3+期末成绩*0.7。
考察的总成绩直接等于期末成绩
实验的总成绩等于课程每次实验成绩的平均分
必修课的考核方式必须为考试,选修课可以选择考试、考察任一考核方式。实验课的成绩必须为实验。
1、输入:
包括课程、课程成绩两类信息。
课程信息包括:课程名称、课程性质、考核方式(可选,如果性质是必修课,考核方式可以没有)三个数据项。
课程信息格式:课程名称+英文空格+课程性质+英文空格+考核方式
课程性质输入项:必修、选修、实验
考核方式输入选项:考试、考察、实验
考试/考查课程成绩信息包括:学号、姓名、课程名称、平时成绩(可选)、期末成绩
考试/考查课程信息格式:学号+英文空格+姓名+英文空格+课程名称+英文空格+平时成绩+英文空格+期末成绩
实验课程成绩信息包括:学号、姓名、课程名称、实验次数、每次成绩
实验次数至少4次,不超过9次
实验课程信息格式:学号+英文空格+姓名+英文空格+课程名称+英文空格+实验次数+英文空格+第一次实验成绩+...+英文空格+最后一次实验成绩
以上信息的相关约束:
1)平时成绩和期末成绩的权重默认为0.3、0.7
2)成绩是整数,不包含小数部分,成绩的取值范围是【0,100】
3)学号由8位数字组成
4)姓名不超过10个字符
5)课程名称不超过10个字符
6)不特别输入班级信息,班级号是学号的前6位。
2、输出:
输出包含三个部分,包括学生所有课程总成绩的平均分、单门课程成绩平均分、单门课程总成绩平均分、班级所有课程总成绩平均分。
为避免误差,平均分的计算方法为累加所有符合条件的单个成绩,最后除以总数。
1)学生课程总成绩平均分按学号由低到高排序输出
格式:学号+英文空格+姓名+英文空格+总成绩平均分
如果某个学生没有任何成绩信息,输出:学号+英文空格+姓名+英文空格+"did not take any exams"
2)单门课程成绩平均分分为三个分值:平时成绩平均分(可选)、期末考试平均分、总成绩平均分,按课程名称的字符顺序输出
考试/考察课程成绩格式:课程名称+英文空格+平时成绩平均分+英文空格+期末考试平均分+英文空格+总成绩平均分
实验课成绩格式:课程名称+英文空格+总成绩平均分
如果某门课程没有任何成绩信息,输出:课程名称+英文空格+"has no grades yet"
3)班级所有课程总成绩平均分按班级由低到高排序输出
格式:班级号+英文空格+总成绩平均分
如果某个班级没有任何成绩信息,输出:班级名称+英文空格+ "has no grades yet"
异常情况:
1)如果解析某个成绩信息时,课程名称不在已输入的课程列表中,输出:学号+英文空格+姓名+英文空格+":"+课程名称+英文空格+"does not exist"
2)如果解析某个成绩信息时,输入的成绩数量和课程的考核方式不匹配,输出:学号+英文空格+姓名+英文空格+": access mode mismatch"
以上两种情况如果同时出现,按第一种情况输出结果。
3)如果解析某个课程信息时,输入的课程性质和课程的考核方式不匹配,输出:课程名称+" : course type & access mode mismatch"
4)格式错误以及其他信息异常如成绩超出范围等,均按格式错误处理,输出"wrong format"
5)若出现重复的课程/成绩信息,只保留第一个课程信息,忽略后面输入的。
信息约束:
1)成绩平均分只取整数部分,小数部分丢弃
import java.text.Collator; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Scanner; public class Main { static boolean isNumber(String isNumber){ try{ int a=Integer.parseInt(isNumber); } catch (NumberFormatException e) { return false; } return true; } public static void main(String[] args) { Scanner input=new Scanner(System.in); String str=""; studentList students=new studentList(); InputMatching matching=new InputMatching(); chooseCourseList choose=new chooseCourseList(); Cls cls=new Cls(); courseList course=new courseList(); String str1,str2,str3; while(true){ str=input.nextLine(); if(isNumber(str.split(" ")[0]))break; if(str.equals("end"))break; if(InputMatching.matchingInput(str) != 1){ System.out.println("wrong format"); continue; } str1=str.split(" ")[0]; if(course.searchCourse(str1))continue; str2=str.split(" ")[1]; str3=str.split(" ")[2]; if((str2.equals("必修")&&str3.equals("考察"))||(str2.equals("实验")&&!str3.equals("实验")||(!str2.equals("实验")&&str3.equals("实验")))){ System.out.println(str.split(" ")[0]+" : course type & access mode mismatch"); continue; } if(course.searchCourse(str1))continue; course.courses.add(course.addCourse(str1,str2,str3)); } String number,name,subject; int UsualGrades,FinalGrades; boolean flag=false; while(!str.equals("end")){ if(flag)str=input.nextLine(); flag=true; if(str.equals("end"))break; String ssr=str.split(" ")[0]+" "+str.split(" ")[1]+" "+str.split(" ")[2]+" "+str.split(" ")[3]; if (InputMatching.matchingInput(ssr) != 2&&InputMatching.matchingInput(ssr) != 3&&course.searchQuality(str.split(" ")[2])!=2) { System.out.println("wrong format");// 字符串符合成绩信息模式 continue; } if(((Integer.parseInt(str.split(" ")[3]))<4||(Integer.parseInt(str.split(" ")[3]))>9)&&str.split(" ").length>5){ System.out.println("wrong format"); continue; } number=str.split(" ")[0]; String Class = number.substring(0, 6); if (!cls.searchCls(Class)) cls.cls.add(cls.addCls(Class)); name=str.split(" ")[1]; students.stu.add(students.addStudent(number,name)); subject=str.split(" ")[2]; try{ if (course.searchQuality(subject) == 1) { UsualGrades = Integer.parseInt(str.split(" ")[3]); if(choose.searchCourse(subject)&&choose.searchId(number))continue; FinalGrades = Integer.parseInt(str.split(" ")[4]); students.students.add(students.addStudentCompulsory(number, name, subject, UsualGrades, FinalGrades)); } else if (course.searchQuality(subject) == 0) { UsualGrades = Integer.parseInt(str.split(" ")[3]); if(choose.searchCourse(subject)&&choose.searchId(number))continue; if(str.split(" ").length>4){System.out.println(number+" "+name+" : access mode mismatch");continue;} students.students.add(students.addStudentTake(number, name, subject, UsualGrades)); } else if (course.searchQuality(subject) == -1) { System.out.println(subject + " does not exist"); } else if(course.searchQuality(subject) == 2){ int testNumber=Integer.parseInt(str.split(" ")[3]); if(str.split(" ").length-4!=testNumber) { System.out.println(number+" "+name+" : access mode mismatch"); continue; } int sum=0; for(int i=0;i<testNumber;i++){ sum+=Integer.parseInt(str.split(" ")[4+i]); } sum/=testNumber; students.students.add(students.addExperimentalGrades(number, name, subject, sum)); } } catch (ArrayIndexOutOfBoundsException e) { System.out.println(number+" "+name+" : access mode mismatch"); } } chooseCourseList chooses=new chooseCourseList(); chooses.getAverageGrade(students,course,cls); } } class student{ int experimentalGrades; String number; String name; String course; int UsualGrades=0; int FinalGrades=0; String Class; int grade=0; public static Comparator<student> getNumberComparator() { return (s1, s2) -> s1.number.compareTo(s2.number); } } class course{ String name; String quality; String manner; public static Comparator<course> getChineseNameComparator() { final Collator collator = Collator.getInstance(java.util.Locale.CHINA); // 获取中文排序规则 return (c1, c2) -> collator.compare(c1.name, c2.name); } } class courseList{ ArrayList<course>courses=new ArrayList<>(); boolean searchCourse(String CourseName){ for (course cours : courses) if (cours.name.equals(CourseName)) return true; return false; } course addCourse( String name,String quality,String manner){ course newCourse=new course(); newCourse.name=name; newCourse.quality=quality; newCourse.manner=manner; return newCourse; } int searchQuality(String subject){ for (course cours : courses) { if (cours.name.equals(subject) && cours.quality.equals("必修")) return 1; else if (cours.name.equals(subject) && cours.manner.equals("考试")) return 1; else if (cours.name.equals(subject)&&cours.quality.equals("实验"))return 2; else if (cours.name.equals(subject)&&cours.manner.equals("实验"))return 2; else if (cours.name.equals(subject)) return 0; } return -1; } } class studentList { ArrayList<student> students = new ArrayList<>(); ArrayList<student> stu = new ArrayList<>(); student addStudentCompulsory(String number, String name, String course, int UsualGrades, int FinalGrades) { student newStudent = new student(); chooseCourseList newChoose = new chooseCourseList(); newStudent.name = name; if(chooseCourseList.searchCourse(course)&& chooseCourseList.chooseCourse.get(chooseCourseList.searchCourseInt(course)).name.equals(name)&& chooseCourseList.chooseCourse.get(chooseCourseList.searchCourseInt(course)).number.equals(number)) return null; Cls newCls = new Cls(); newStudent.number = number; newStudent.course = course; newStudent.grade = (int) (FinalGrades * 0.7 + UsualGrades * 0.3); String Class = number.substring(0, 6); if (!newCls.searchCls(Class)) newCls.cls.add(newCls.addCls(Class)); chooseCourseList.chooseCourse.add(newChoose.addChoose(number, name, course, Class, UsualGrades, FinalGrades, newStudent.grade)); return newStudent; } student addStudentTake(String number, String name, String course, int UsualGrades) { student newStudent = new student(); newStudent.name = name; newStudent.number = number; newStudent.course = course; newStudent.grade = UsualGrades; String Class=number.substring(0,6); chooseCourseList newChoose = new chooseCourseList(); if(chooseCourseList.searchCourse(course)&& chooseCourseList.chooseCourse.get(chooseCourseList.searchCourseInt(course)).name.equals(name)&& chooseCourseList.chooseCourse.get(chooseCourseList.searchCourseInt(course)).number.equals(number)) return null; chooseCourseList.chooseCourse.add(newChoose.addChooseTake(number, name, course, Class, UsualGrades, newStudent.grade)); return newStudent; } student addExperimentalGrades(String number, String name, String course, int experimentalGrades) { student newStudent = new student(); newStudent.name = name; newStudent.number = number; newStudent.course = course; newStudent.experimentalGrades = experimentalGrades; newStudent.grade = experimentalGrades; String Class=number.substring(0,6); chooseCourseList newChoose = new chooseCourseList(); if(chooseCourseList.searchCourse(course)&& chooseCourseList.chooseCourse.get(chooseCourseList.searchCourseInt(course)).name.equals(name)&& chooseCourseList.chooseCourse.get(chooseCourseList.searchCourseInt(course)).number.equals(number)) return null; chooseCourseList.chooseCourse.add(newChoose.addChooseExperimental(number, name, course, Class, experimentalGrades,newStudent.grade)); return newStudent; } student addStudent(String id, String name) { student newStudent = new student(); newStudent.number = id; newStudent.name = name; return newStudent; } boolean searchStudent(String name) { for (student student : students) { if (student.name.equals(name)) return true; } return false; } } class Cls { private static int nextIndex = 0; String id; private int index; ArrayList<Cls> cls = new ArrayList<>(); public Cls addCls(String cl) { Cls newCls = new Cls(); newCls.id = cl; // 新增元素的 index 应该为当前列表长度 newCls.index = nextIndex++; cls.add(newCls); return newCls; } public boolean searchCls(String id) { for (int i = 0; i < cls.size(); i++) { if (cls.get(i).id.equals(id)) { return true; } } return false; } public static Comparator<Cls> getIndexComparator() { return new Comparator<Cls>() { @Override public int compare(Cls c1, Cls c2) { return c1.id.compareTo(c2.id); } }; } } class InputMatching { static String stuNumMatching = "[0-9]{8}";//8个0-9的数字 static String stuNameMatching = "\\S{1,10}";//1到10个非空格(TAB)字符 static String scoreMatching = "([1-9]?[0-9]|100)"; static String courseNameMatching = "\\S{1,10}";//1到10个非空格(TAB)字符 static String courseTypeMatching = "(选修|必修|实验)"; static String checkcourseTypeMatching = "(考试|考察|实验)"; //cousrInput用于定义课程信息模式(正则表达式) static String courseInput = courseNameMatching + " " + courseTypeMatching + " " + checkcourseTypeMatching; //scoreInput用于定义成绩信息模式(正则表达式) static String scoreInput1 = stuNumMatching + " " + stuNameMatching + " " + courseNameMatching + " " + scoreMatching + "(scoreMatching)?"; static String scoreInput2 = stuNumMatching + " " + stuNameMatching + " " + courseNameMatching + " " + scoreMatching + "(scoreMatching)?"+" "+scoreMatching + "(scoreMatching)?"; public static int matchingInput(String s) { if (matchingCourse(s)) { return 1; } if (matchingScore1(s)) { return 2; } if (matchingScore2(s)) { return 3; } return 0; } static boolean matchingCourse(String s) { return s.matches(courseInput); } static boolean matchingScore1(String s) { return s.matches(scoreInput1); } static boolean matchingScore2(String s) { return s.matches(scoreInput2); } } class chooseCourseList{ static ArrayList<student> chooseCourse=new ArrayList<>(); static boolean searchId(String id){ for (student student : chooseCourse) if (student.number.equals(id)) return true; return false; } static boolean searchCourse(String course){ for(int i=0;i<chooseCourse.size();i++) if(chooseCourse.get(i).course.equals(course))return true; return false; } static int searchCourseInt(String course){ for(int i=0;i<chooseCourse.size();i++) if(chooseCourse.get(i).course.equals(course))return i; return -1; } static boolean searchClass(String Class){ for(int i=0;i<chooseCourse.size();i++) if(chooseCourse.get(i).Class.equals(Class))return true; return false; } student addChoose(String id,String name,String course,String Class,int UsualGrades,int FinalGrades,int grade ) { student newStudent=new student(); newStudent.Class=Class; newStudent.number=id; newStudent.name=name; newStudent.course=course; newStudent.UsualGrades=UsualGrades; newStudent.FinalGrades=FinalGrades; newStudent.grade=grade; return newStudent; } student addChooseTake(String id,String name,String course,String Class,int UsualGrades,int grade ) { student newStudent=new student(); newStudent.Class=Class; newStudent.number=id; newStudent.name=name; newStudent.course=course; newStudent.UsualGrades=UsualGrades; newStudent.grade=grade; return newStudent; } student addChooseExperimental(String id,String name,String course,String Class,int experimental,int grade ) { student newStudent=new student(); newStudent.Class=Class; newStudent.number=id; newStudent.name=name; newStudent.course=course; newStudent.experimentalGrades=experimental; newStudent.grade=experimental; return newStudent; } public int getAverageGrades(String id){ int sum=0,cnt=0; for(int i=0;i<chooseCourse.size();i++){ if(chooseCourse.get(i).number.equals(id)){sum+=chooseCourse.get(i).grade;cnt++;} } return sum/=cnt; } public int getAverageCourseUsualGrades(String course){ int sum=0,cnt=0; for(int i=0;i<chooseCourse.size();i++){ if(chooseCourse.get(i).course.equals(course)){sum+=chooseCourse.get(i).UsualGrades;cnt++;} } return sum/=cnt; } public int getAverageCourseFinalGrades(String course){ int sum=0,cnt=0; for(int i=0;i<chooseCourse.size();i++){ if(chooseCourse.get(i).course.equals(course)){sum+=chooseCourse.get(i).FinalGrades;cnt++;} } return sum/=cnt; } public int getAverageCourseGrades(String course){ int sum=0,cnt=0; for(int i=0;i<chooseCourse.size();i++){ if(chooseCourse.get(i).course.equals(course)){sum+=chooseCourse.get(i).grade;cnt++;} } return sum/=cnt; } public int getAverageExperimentalGrades(String course){ int sum=0,cnt=0; for(int i=0;i<chooseCourse.size();i++){ if(chooseCourse.get(i).course.equals(course)){sum+=chooseCourse.get(i).experimentalGrades;cnt++;} } return sum/=cnt; } public int getClassAverageGrades(String Class){ int sum=0,cnt=0; for(int i=0;i<chooseCourse.size();i++){ if(chooseCourse.get(i).Class.equals(Class)){sum+=chooseCourse.get(i).grade;cnt++;} } return sum/=cnt; } public void getAverageGrade(studentList students,courseList courses,Cls cls){ Collections.sort(students.stu, student.getNumberComparator()); Collections.sort(cls.cls, Cls.getIndexComparator()); Collections.sort(courses.courses, course.getChineseNameComparator()); for(int i=0;i<students.stu.size();i++){ int flag=0; for(int j=0;j<i;j++){ if(students.stu.get(j).number.equals(students.stu.get(i).number)){flag=1;break;} } if(flag==1)continue; if(searchId(students.stu.get(i).number)){ System.out.println(students.stu.get(i).number+" "+students.stu.get(i).name+" "+getAverageGrades(students.stu.get(i).number)); } else{ System.out.println(students.stu.get(i).number+" "+students.stu.get(i).name+" did not take any exams"); } } for(int i=0;i<courses.courses.size();i++){ if(searchCourse(courses.courses.get(i).name)){ if(courses.courses.get(i).manner.equals("考试")){ System.out.println(courses.courses.get(i).name+" "+getAverageCourseUsualGrades(courses.courses.get(i).name) +" "+getAverageCourseFinalGrades(courses.courses.get(i).name)+" "+getAverageCourseGrades(courses.courses.get(i).name)); } else if(courses.courses.get(i).manner.equals("考察"))System.out.println(courses.courses.get(i).name+" "+getAverageCourseUsualGrades(courses.courses.get(i).name) +" "+getAverageCourseGrades(courses.courses.get(i).name)); else System.out.println(courses.courses.get(i).name+" "+getAverageExperimentalGrades(courses.courses.get(i).name)); } else{ System.out.println(courses.courses.get(i).name+" has no grades yet"); } } for(int i=0;i<cls.cls.size();i++){ int flag=0; for(int j=0;j<i;j++){ if(cls.cls.get(j).equals(cls.cls.get(i))){flag=1;break;} } if(flag==1)continue; if(searchClass(cls.cls.get(i).id)){ System.out.println(cls.cls.get(i).id+" "+getClassAverageGrades(cls.cls.get(i).id)); } else{ System.out.println(cls.cls.get(i).id+" has no grades yet"); } } } }
8-3:
- 输入n,然后连续输入n个身份证号。
- 然后根据输入的是sort1还是sort2,执行不同的功能。输入的不是sort1或sort2,则输出
exit
并退出。
输入sort1,将每个身份证的年月日抽取出来,按年-月-日格式组装,然后对组装后的年-月-日升序输出。
输入sort2,将所有身份证按照里面的年月日升序输出import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n; n = scanner.nextInt(); scanner.nextLine(); // 读取换行符 List<String> idCards = new ArrayList<>(); for (int i = 0; i < n; i++) { String idCard = scanner.nextLine(); idCards.add(idCard); } while(true){ String sortType = scanner.nextLine(); if(sortType.equals("e")){ System.out.println("exit"); break; } if (sortType.equals("sort1")) { sortIdCardsByDate(idCards); } else if (sortType.equals("sort2")) { sortIdCardsByFullDate(idCards); } else { System.out.println("exit"); return; } } } private static void sortIdCardsByDate(List<String> idCards) { List<Date> dates = new ArrayList<>(); SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); for (String idCard : idCards) { String dateStr = idCard.substring(6, 14); try { Date date = format.parse(dateStr); dates.add(date); } catch (ParseException e) { e.printStackTrace(); } } Collections.sort(dates); List<String> sortedIdCards = new ArrayList<>(); SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd"); for (Date date : dates) { String formattedDate = outputFormat.format(date); System.out.println(formattedDate); for (String idCard : idCards) { if (idCard.contains(formattedDate)) { sortedIdCards.add(idCard); } } } for (String idCard : sortedIdCards) { System.out.println(idCard); } } // 按日期排序身份证号码 private static void sortIdCardsByFullDate(List<String> idCards) { // 创建一个自定义的Comparator匿名内部类,按照年月日升序排序 Comparator<String> idCardComparator = new Comparator<String>() { @Override public int compare(String idCard1, String idCard2) { String dateStr1 = idCard1.substring(6, 14); String dateStr2 = idCard2.substring(6, 14); return dateStr1.compareTo(dateStr2); } }; Collections.sort(idCards, idCardComparator); for (String idCard : idCards) { System.out.println(idCard); } } }
8-1:
-
输入多个学生的成绩信息,包括:学号、姓名、数学成绩、物理成绩。
学号是每个学生的唯一识别号,互不相同。
姓名可能会存在重复。
要求:使用ArrayList存储学生信息
-
import java.util.*; class Student implements Comparable<Student> { private String studentId; private String name; private int mathScore; private int physicsScore; private int totalScore; public Student(String studentId, String name, int mathScore, int physicsScore) { this.studentId = studentId; this.name = name; this.mathScore = mathScore; this.physicsScore = physicsScore; this.totalScore = mathScore + physicsScore; } public String getStudentId() { return studentId; } public String getName() { return name; } public int getTotalScore() { return totalScore; } @Override public int compareTo(Student other) { // 按照数学和物理成绩之和从高到低排序 if (this.totalScore != other.totalScore) { return Integer.compare(other.totalScore, this.totalScore); } else { // 如果成绩相同,按输入的先后顺序排序 return 0; } } } class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); List<Student> students = new ArrayList<>(); while (true) { String input = scanner.nextLine(); if (input.equals("end")) { break; } String[] info = input.split(" "); String studentId = info[0]; String name = info[1]; int mathScore = Integer.parseInt(info[2]); int physicsScore = Integer.parseInt(info[3]); students.add(new Student(studentId, name, mathScore, physicsScore)); } Collections.sort(students); for (Student student : students) { System.out.println(student.getStudentId() + " " + student.getName() + " " + student.getTotalScore()); } } }