输入多个学生的成绩信息,包括:学号、姓名、成绩。
学号是每个学生的唯一识别号,互不相同。
姓名可能会存在重复。
使用HashMap存储学生信息,并实现根据学号的检索功能
输入格式:
输入多个学生的成绩信息,每个学生的成绩信息格式:学号+英文空格+姓名+英文空格+成绩
以“end”为输入结束标志
end之后输入某个学号,执行程序输出该生的详细信息
输出格式:
输出查询到的学生信息格式:学号+英文空格+姓名+英文空格+成绩
如果没有查询到,则输出:"The student "+查询的学号+" does not exist"
输入样例1:
在这里给出一组输入。例如:
20201107 张少军 83
20201116 李四 78
20201118 郑觉先 80
end
20201116
输出样例1:
在这里给出相应的输出。例如:
20201116 李四 78
输入样例2:
在这里给出一组输入。例如:
20201107 张少军 83
20201116 李四 78
20201118 郑觉先 80
end
20202316
输出样例2:
在这里给出相应的输出。例如:
The student 20202316 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, String> map = new HashMap<>(); while (true) { String input = scanner.nextLine(); if (input.equals("end")) { break; } String[] info = input.split(" "); map.put(info[0], info[1] + " " + info[2]); } String query = scanner.nextLine(); if (map.containsKey(query)) { System.out.println(query + " " + map.get(query)); } else { System.out.println("The student " + query + " does not exist"); } } }
输入多个学生的成绩信息,包括:学号、姓名、成绩。
学号是每个学生的唯一识别号,互不相同。
姓名可能会存在重复。
要求:使用HashMap存储学生信息。
输入格式:
输入多个学生的成绩信息,每个学生的成绩信息格式:学号+英文空格+姓名+英文空格+成绩
以“end”为输入结束标志
输出格式:
按学号从大到小的顺序输出所有学生信息,每个学生信息的输出格式:学号+英文空格+姓名+英文空格+成绩
输入样例:
在这里给出一组输入。例如:
20201124 张少军 83
20201136 李四 78
20201118 郑觉先 80
end
输出样例:
在这里给出相应的输出。例如:
20201136 李四 78
20201124 张少军 83
20201118 郑觉先 80
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Map<String, String> map = new HashMap<>(); while (sc.hasNextLine()) { String input = sc.nextLine(); if (input.equals("end")) { break; } String[] inputs = input.split(" "); map.put(inputs[0], inputs[1] + " " + inputs[2]); } List<Map.Entry<String, String>> list = new ArrayList<>(map.entrySet()); Collections.sort(list, (o1, o2) -> Integer.parseInt(o2.getKey()) - Integer.parseInt(o1.getKey())); for (Map.Entry<String, String> entry : list) { System.out.println(entry.getKey() + " " + entry.getValue()); } } }
课程成绩统计程序-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)成绩平均分只取整数部分,小数部分丢弃
参考类图(与第一次相同,其余内容自行补充):
输入样例1:
在这里给出一组输入。例如:
java 实验 实验
20201103 张三 java 4 70 80 90
end
输出样例1:
在这里给出相应的输出。例如:
20201103 张三 : access mode mismatch 20201103 张三 did not take any exams java has no grades yet 202011 has no grades yet
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.text.Collator; import java.util.*; import java.util.stream.Collectors; public class Main { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); MessageGetAndPrint messageGetAndPrint = new MessageGetAndPrint(); messageGetAndPrint.print = new Print(); for(;;){ String message = in.readLine(); if(message.equals("end")) { messageGetAndPrint.print.personPrint(messageGetAndPrint.students , messageGetAndPrint.courseSelections); messageGetAndPrint.print.coursePrint(messageGetAndPrint.courses , messageGetAndPrint.courseSelections); messageGetAndPrint.print.classPrint(messageGetAndPrint.classes , messageGetAndPrint.courseSelections); break; } messageGetAndPrint.getMessage(message); } } } class Class { private String classNum; HashMap<String , Student> students; Class(String classNum) { setClassNum(classNum); } public void setClassNum(String classNum) { this.classNum = classNum; } public String getClassNum() { return classNum; } public void addStudent(Student student) { students.put(student.getStudentNum() , student); } public void deleteStudent(String studentNum) { students.remove(studentNum); } } class Student { private String name; private String studentNum; Student(String name , String studentNum) { setName(name); setStudentNum(studentNum); } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getStudentNum() { return studentNum; } public void setStudentNum(String studentNum) { this.studentNum = studentNum; } } class Course { private String className; private int classQuality; private int assessmentMethodology; Course(String className , int classQuality , int assessmentMethodology) { setClassName(className); setClassQuality(classQuality); setAssessmentMethodology(assessmentMethodology); } public String getClassName() { return className; } public void setClassName(String className) { this.className = className; } public int getClassQuality() { return classQuality; } public void setClassQuality(int classQuality) { this.classQuality = classQuality; } public int getAssessmentMethodology() { return assessmentMethodology; } public void setAssessmentMethodology(int assessmentMethodology) { this.assessmentMethodology = assessmentMethodology; } } class CourseSelection { Student student; Course course; Grade grade; } abstract class Grade { int allGrade; abstract void getAllGrade(); } class GradeA extends Grade { private int finalGrade; private int usualGrade; GradeA(int finalGrade , int usualGrade) { setFinalGrade(finalGrade); setUsualGrade(usualGrade); } public void setFinalGrade(int finalGrade) { this.finalGrade = finalGrade; } public int getFinalGrade() { return finalGrade; } public void setUsualGrade(int usualGrade) { this.usualGrade = usualGrade; } public int getUsualGrade() { return usualGrade; } @Override void getAllGrade() { allGrade = (int) (finalGrade*0.7+usualGrade*0.3); } } class GradeB extends Grade { private int finalGrade; GradeB(int finalGrade) { setFinalGrade(finalGrade); } public void setFinalGrade(int finalGrade) { this.finalGrade = finalGrade; } public int getFinalGrade() { return finalGrade; } @Override void getAllGrade() { allGrade = finalGrade; } } class GradeC extends Grade { ArrayList<Integer> usualGrades; GradeC(ArrayList<Integer> usualGrades) { this.usualGrades = usualGrades; } @Override void getAllGrade() { allGrade = 0; for(int x:usualGrades){ allGrade+=x; } allGrade = allGrade/usualGrades.size(); } } class Print { public void personPrint(HashMap<String , Student> students , ArrayList<CourseSelection> courseSelections) { Set<String> set = students.keySet(); Object[] arr=set.toArray(); Arrays.sort(arr); for(Object key:arr) { int x = 0 , y = -1; for (CourseSelection courseSelection : courseSelections) { if (students.get(key).getStudentNum().equals(courseSelection.student.getStudentNum())) { if(y == -1) y = 0; courseSelection.grade.getAllGrade(); x += courseSelection.grade.allGrade; y++; } } if(y == -1) { System.out.println(students.get(key).getStudentNum()+" "+students.get(key).getName()+" did not take any exams"); continue; } x = x / y; System.out.println(students.get(key).getStudentNum()+" "+students.get(key).getName()+" "+x); } } public void coursePrint(HashMap<String , Course> courses , ArrayList<CourseSelection> courseSelections) { List<String> list = courses.keySet().stream().collect(Collectors.toList()); Collections.sort(list , new SoftName()); for(String key:list) { int x = 0 , y = -1 , f = 0 , u = 0 , uy = -1 , s = -1; for (CourseSelection courseSelection : courseSelections) { if (courses.get(key).getClassName().equals(courseSelection.course.getClassName())) { if(y == -1) y = 0; if(courses.get(key).getAssessmentMethodology() == 1 && uy == -1) uy = 0; if(courses.get(key).getAssessmentMethodology() == 3 && s == -1) s = 0; courseSelection.grade.getAllGrade(); x += courseSelection.grade.allGrade; if(courseSelection.grade instanceof GradeA) { f += ((GradeA) courseSelection.grade).getFinalGrade(); u += ((GradeA) courseSelection.grade).getUsualGrade(); uy++; } if(courseSelection.grade instanceof GradeB) { f += ((GradeB) courseSelection.grade).getFinalGrade(); } y++; } } if(y == -1) { System.out.println(courses.get(key).getClassName()+" has no grades yet"); continue; } x = x/y; f = f/y; if(s == 0) { System.out.println(courses.get(key).getClassName()+" "+x); continue; } if(uy == -1) { System.out.println(courses.get(key).getClassName()+" "+f+" "+x); } if(uy != -1) { u = u/uy; System.out.println(courses.get(key).getClassName()+" "+u+" "+f+" "+x); } } } public void classPrint(HashMap<String , Class> classes , ArrayList<CourseSelection> courseSelections) { Set<String> set = classes.keySet(); Object[] arr=set.toArray(); Arrays.sort(arr); for(Object key:arr) { int x = 0 , y = -1; for(CourseSelection courseSelection : courseSelections) { if(classes.get(key).students.containsKey(courseSelection.student.getStudentNum())) { if(y == -1) { y = 0; } courseSelection.grade.getAllGrade(); x += courseSelection.grade.allGrade; y++; } } if(y == -1) { System.out.println(classes.get(key).getClassNum()+" has no grades yet"); continue; } x = x/y; System.out.println(classes.get(key).getClassNum()+" "+x); } } public void wrongFormat() { System.out.println("wrong format"); } public void courseExist(String name) { System.out.println(name+" does not exist"); } public void courseMatch(String studentNum , String studentName) { System.out.println(studentNum+" "+studentName+" : access mode mismatch"); } public void courseMatch(String name) { System.out.println(name+" : course type & access mode mismatch"); } static class SoftName implements Comparator<String>{ public int compare(String name1 , String name2) { Comparator<Object> compare = Collator.getInstance(Locale.CHINA); return compare.compare(name1 , name2); } } } class MessageGetAndPrint { Print print; HashMap<String , Class> classes = new HashMap<>(); HashMap<String , Student> students = new HashMap<>(); HashMap<String , Course> courses = new HashMap<>(); ArrayList<CourseSelection> courseSelections = new ArrayList<>(); public void getMessage(String getInput) { String[] message = getInput.split(" "); if(getInput.matches("^(\\S{1,10})( )(必修)$")) { int i = 1, j = 1; if(courses.containsKey(message[0])) { return; } Course course = new Course(message[0], i, j); courses.put(message[0], course); return; } if(getInput.matches("^(\\S{1,10})( )(必修|选修|实验)( )(考试|考察|实验)$")) { int i = 0, j = 0; if(message[1].equals("必修")) { i = 1; } if(message[1].equals("选修")) { i = 2; } if(message[1].equals("实验")) { i = 3; } if(message[2].equals("考试")) { j = 1; } if(message[2].equals("考察")) { j = 2; } if(message[2].equals("实验")) { j = 3; } if(i == 1 && j != 1) { print.courseMatch(message[0]); return; } if(i == 3 && j != 3) { print.courseMatch(message[0]); return; } if(i != 3 && j == 3) { print.courseMatch(message[0]); return; } if(courses.containsKey(message[0])) { return; } Course course = new Course(message[0], i, j); courses.put(message[0], course); return; } if(getInput.matches("^([0-9]{8})( )(\\S{1,10})( )(\\S{1,10})( )([0-9]|[1-9][0-9]|100)( )([0-9]|[1-9][0-9]|100)$")) { for(CourseSelection courseSelection:courseSelections) { if(courseSelection.student.getStudentNum().equals(message[0])&&courseSelection.student.getName().equals(message[1])&&courseSelection.course.getClassName().equals(message[2])) { return; } } Student student = new Student(message[1], message[0]); students.put(message[0] , student); String classNum = message[0].substring(0 , 6); if(!classes.containsKey(classNum)) { Class aClass = new Class(classNum); classes.put(classNum , aClass); classes.get(classNum).students = new HashMap<>(); } classes.get(classNum).students.put(message[0], student); if(!courses.containsKey(message[2])) { print.courseExist(message[2]); return; } if(courses.get(message[2]).getAssessmentMethodology() != 1) { print.courseMatch(message[0] , message[1]); return; } CourseSelection courseSelection = new CourseSelection(); courseSelection.student = student; courseSelection.course = courses.get(message[2]); int finalGrade = Integer.parseInt(message[4]); int usualGrade = Integer.parseInt(message[3]); courseSelection.grade = new GradeA(finalGrade , usualGrade); courseSelections.add(courseSelection); return; } if(getInput.matches("^([0-9]{8})( )(\\S{1,10})( )(\\S{1,10})( )([0-9]|[1-9][0-9]|10{2})$")) { for(CourseSelection courseSelection:courseSelections) { if(courseSelection.student.getStudentNum().equals(message[0])&&courseSelection.student.getName().equals(message[1])&&courseSelection.course.getClassName().equals(message[2])) { return; } } Student student = new Student(message[1], message[0]); students.put(message[0] , student); String classNum = message[0].substring(0 , 6); if(!classes.containsKey(classNum)) { Class aClass = new Class(classNum); classes.put(classNum , aClass); classes.get(classNum).students = new HashMap<>(); } classes.get(classNum).addStudent(student); if(!courses.containsKey(message[2])) { print.courseExist(message[2]); return; } if(courses.get(message[2]).getAssessmentMethodology() != 2) { print.courseMatch(message[0] , message[1]); return; } CourseSelection courseSelection = new CourseSelection(); courseSelection.student = student; courseSelection.course = courses.get(message[2]); int finalGrade = Integer.parseInt(message[3]); courseSelection.grade = new GradeB(finalGrade); courseSelections.add(courseSelection); return; } if(getInput.matches("^([0-9]{8})( )(\\S{1,10})( )(\\S{1,10})( )([4-9])( )((([0-9]|[1-9][0-9]|100)( ))+)([0-9]|[1-9][0-9]|100)$")) { for(CourseSelection courseSelection:courseSelections) { if(courseSelection.student.getStudentNum().equals(message[0])&&courseSelection.student.getName().equals(message[1])&&courseSelection.course.getClassName().equals(message[2])) { return; } } Student student = new Student(message[1], message[0]); students.put(message[0] , student); String classNum = message[0].substring(0 , 6); if(!classes.containsKey(classNum)) { Class aClass = new Class(classNum); classes.put(classNum , aClass); classes.get(classNum).students = new HashMap<>(); } classes.get(classNum).addStudent(student); if(!courses.containsKey(message[2])) { print.courseExist(message[2]); return; } if(courses.get(message[2]).getAssessmentMethodology() != 3) { print.courseMatch(message[0] , message[1]); return; } int usualNum = Integer.parseInt(message[3]); if(message.length-4 != usualNum) { print.courseMatch(message[0] , message[1]); return; } CourseSelection courseSelection = new CourseSelection(); courseSelection.student = student; courseSelection.course = courses.get(message[2]); ArrayList<Integer> usualGrades = new ArrayList<>(); for(int i = 1;i <= usualNum;i++) { usualGrades.add(Integer.parseInt(message[3+i])); } courseSelection.grade = new GradeC(usualGrades); courseSelections.add(courseSelection); return; } print.wrongFormat(); } }