系统需求:
单个学生答案文件:
- 评分单个学生
选择单个学生评分,需要输入正【确答案的路径】和【学生答案的路径】,截图如下
- 查看成绩信息
输入1,查看该学生的成绩信息,如图
- 输出成绩到文件
选择2,输出成绩到文件中,并提示文件路径
输出成功后,对应的文件夹会生成该学生的成绩信息,文本内容如下
- 回到主菜单
回到初始化菜单
- 评分全部学生
也需要输入文件的路径,如果文件错误给出相应的提示
错误提示
- 查看成绩信息
- 查看成绩排名
- 查看错题排行
统计错误最多的题目
- 输出成绩到文件
文件信息
代码:
文件代码:Java计算机语言基础实现学生评分系统(仅使用IO流实现)-Java文档类资源-CSDN文库
import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.*; public class Text { //全局Scanner static Scanner sc = new Scanner(System.in); public static void main(String[] args) throws IOException { run(); } /** * 运行架构 */ public static void run() throws IOException { while (true) { System.out.println("请选择功能"); System.out.println("1.评分单个学生"); System.out.println("2.评分全部学生"); System.out.println("3.退出系统"); switch (sc.nextInt()) { case 1: singleStudent(); break; case 2: allStudent(); break; case 3: System.out.println("退出成功,感谢您的使用"); return; default: System.out.println("无该功能,请您重新输入"); } } } /** * 单个学生成绩评分 * * @return @return 返回解析后的学生对象 */ private static void singleStudent() throws IOException { //判断路径是否正确,如果不正确,重新输入 String templatePath; while (true) { System.out.print("请输入正确答案路径(文件):"); //输入正确答案路径 templatePath = sc.next(); File file = new File(templatePath); if (file.isFile()) { break; } else { System.out.println("路径不正确,必须是文件,请您重新输入"); } } //判断路径是否正确,如果不正确,重新输入 String studentPath; while (true) { System.out.print("请输入学生答案路径(文件):"); studentPath = sc.next(); if (new File(studentPath).isFile()) { break; } else { System.out.println("路径不正确,必须是文件,请您重新输入"); } } //解析答案信息 Student student = ScoreUtils.startSingle(templatePath, studentPath); while (true) { //解析完成,进入菜单 System.out.println("解析完成,请选择操作:"); System.out.println("1.查看成绩信息"); System.out.println("2.输出成绩到文件"); System.out.println("3.返回主菜单"); switch (sc.nextInt()) { case 1: //输出学生成绩信息 System.out.println("1.查看学生成绩信息:"); System.out.println(student.getName() + "的成绩为:" + student.getScore() + "分"); break; case 2: //将学生的考试信息保存到文件中 System.out.println("2.输出成绩到文件:"); saveFile("single完成", student); break; case 3: System.out.println("返回成功!"); return; default: System.out.println("无该功能,请重新选择"); break; } } } /** * 将学生的考试信息保存到文件中 * * @param student */ private static void saveFile(String path, Student student) throws IOException { File file = new File(path + "\\" + student.getName() + ".txt"); //创建文件时,如果文件夹不存在,那么主动创建文件夹和文件 //1.创建file父目录的文件对象 File parentFile = file.getParentFile(); //2.若file父目录不存在,就创建 if (!parentFile.exists()) { parentFile.mkdirs(); } //3.如果file文件不能存在,就创建 if (!file.exists()) { file.createNewFile(); } //使用字符缓冲输出流给文件写入信息 BufferedWriter bw = new BufferedWriter(new FileWriter(file.getAbsolutePath())); bw.write("姓名:" + student.getName() + " 总分:" + student.getScore() + "分"); bw.newLine(); for (Topic topic : student.getTopics()) { bw.write(topic.toString()); bw.newLine(); bw.flush(); } bw.close(); System.out.println("完成,文件路径为:" + file.getAbsolutePath()); } private static void allStudent() throws IOException { //判断路径是否正确,如果不正确,重新输入 String templatePath; while (true) { System.out.print("请输入正确答案路径(文件):"); //输入正确答案路径 templatePath = sc.next(); File file = new File(templatePath); if (file.isFile()) { break; } else { System.out.println("路径不正确,必须是文件,请您重新输入"); } } //判断路径是否正确,如果不正确,重新输入 String studentPaths; while (true) { System.out.print("请输入学生答案路径(文件夹):"); studentPaths = sc.next(); if (new File(studentPaths).isDirectory()) { break; } else { System.out.println("路径不正确,必须是文件夹,请您重新输入"); } } //解析所有学生答案信息 ArrayList<Student> students = ScoreUtils.startAll(templatePath, studentPaths); while (true) { //解析完成,进入菜单 System.out.println("解析完成,请选择操作:"); System.out.println("1.查看成绩信息"); System.out.println("2.查看成绩排名"); System.out.println("3.查看错题信息(排名)"); System.out.println("4.输出成绩到文件"); System.out.println("5.返回主菜单"); switch (sc.nextInt()) { case 1: System.out.println("1.查看成绩信息:"); for (Student student : students) { System.out.println("姓名:" + student.getName() + " 总分:" + student.getScore()); } break; case 2: //自定义排序规则 Collections.sort(students, new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { return (int) (o2.getScore() - o1.getScore()); } }); System.out.println("2.查看成绩排名:"); for (Student student : students) { System.out.println("姓名:" + student.getName() + " 总分:" + student.getScore()); } break; case 3: System.out.println("3.查看错题信息(排名):"); errorSort(students); break; case 4: for (Student student : students) { saveFile("all完成", student); } System.out.println("4.输出成绩到文件:"); break; case 5: System.out.println("返回成功!"); return; default: System.out.println("无该功能,请重新输入"); break; } } } //错题排名 private static void errorSort(ArrayList<Student> students) { //统计错题个数 //key:题目编号 //value:错题个数 Map<Integer, Integer> errorMap = new HashMap<>(); for (Student student : students) { //获取学生所有题目 ArrayList<Topic> topics = student.getTopics(); for (Topic topic : topics) { if (topic.getTopicScore() == 0) { if (errorMap.containsKey(topic.getTopicNumber())) { errorMap.put(topic.getTopicNumber(), errorMap.get(topic.getTopicNumber()) + 1); } else { errorMap.put(topic.getTopicNumber(), 1); } } } } /* map排序 1.先将map转成list: List<Map.Entry<Integer,Integer>> list = new ArrayList<>(errorMap.entrySet()); 2.利用Collections.sort()对list进行排序: Collections.sort(list, (o1, o2) -> o2.getValue() - o1.getValue()); 3.将list转成map,利用LinkedHashMap按顺序插入数据的特点 LinkedHashMap<Integer, Integer> linkedHashMap = new LinkedHashMap<>(); for (Map.Entry<Integer, Integer> entry : list) { linkedHashMap.put(entry.getKey(), entry.getValue()); } */ List<Map.Entry<Integer, Integer>> list = new ArrayList<>(errorMap.entrySet()); Collections.sort(list, (o1, o2) -> o2.getValue() - o1.getValue()); LinkedHashMap<Integer, Integer> linkedHashMap = new LinkedHashMap<>(); for (Map.Entry<Integer, Integer> entry : list) { linkedHashMap.put(entry.getKey(), entry.getValue()); } for (Integer key : linkedHashMap.keySet()) { System.out.println("错题编号" + key + " 错题个数为:" + errorMap.get(key)); } } }
import java.io.*; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; public class ScoreUtils { static HashMap<Integer, String> correctAnswerMap; /** * 解析单个学生的考试信息 * * @param templatePath 正确答案文件路径 * @param studentPath 学生答案文件路径 * @return 返回解析后的学生对象 */ public static Student startSingle(String templatePath, String studentPath) throws IOException { //获取答案数据 correctAnswerMap= parseAnswer(templatePath); Student student = disposeTopic(studentPath); return student; } /** * 处理学生信息 * @param studentPath * @return 返回解析后的学生对象 * @throws IOException */ private static Student disposeTopic(String studentPath) throws IOException { HashMap<Integer, String> studentAnswerMap = parseAnswer(studentPath); //从学生文件路径获取学生姓名 File file = new File(studentPath); String name = file.getName().split("\\.")[0]; Student student = new Student(); //设置学生姓名 student.setName(name); //设置学生总分 double totalScore = 0; //存储答题信息 ArrayList<Topic> topics = new ArrayList<>(); for (int i = 1; i <= studentAnswerMap.size(); i++) { //获取正确答案 String correctAnswer = correctAnswerMap.get(i); //获取学生信息 String studentAnswer = studentAnswerMap.get(i); //获取题目编号,正确答案,你的选项,得分 Topic topic = new Topic(i, correctAnswer, studentAnswer, 0); if (correctAnswer.equals(studentAnswer)) { //正确,一题为2.5分 topic.setTopicScore(2.5); totalScore += 2.5; } topics.add(topic); } //设置学生总分 student.setScore(totalScore); //设置学生的答题信息 student.setTopics(topics); return student; } /** * 解析答案数据(存储答案数据) * * @param answerPath 答案文件路径 * @return 返回解析后的答案数据HashMap集合 */ public static HashMap<Integer, String> parseAnswer(String answerPath) throws IOException { //存储解析后的答案数据 HashMap<Integer, String> tempAnswer = new HashMap<>(); //1. 使用IO流读取数据(字符流按行读取) BufferedReader br = new BufferedReader(new FileReader(answerPath)); //记录一行信息 String line; while ((line = br.readLine()) != null) { //System.out.println(line); //2. 使用:进行分割split(":") String[] temp = line.split(":"); //3.使用-进行分割split("-"),获取编号数组 String[] numberArr = temp[0].split("-"); //4.使用,进行分割split(","),获取答案数组 String[] answerArr = temp[1].split(","); //5. 遍历答案数组,将数据存入集合中 //题目编号的开始 int startNumber = Integer.parseInt(numberArr[0]); for (int i = 0; i < answerArr.length; i++) { //获取未排序的答案 String answer = answerArr[i]; //对多选题的答案进行排序 if (answerArr[i].length() > 1) { //String --> byte[] byte[] bytes = answer.getBytes(); //排序多选题数组的答案 Arrays.sort(bytes); //将排序后的数组转成String类型 answer = new String(bytes); } tempAnswer.put(startNumber + i, answer); } } return tempAnswer; } /** * 评分全部学生 * @param templatePath 正确答案路径 * @param studentPaths 学生答案文件夹路径 */ public static ArrayList<Student> startAll(String templatePath, String studentPaths) throws IOException { //获取答案数据 correctAnswerMap = parseAnswer(templatePath); //获取文件夹中的所有学生的文件 File studentFiles = new File(studentPaths); File[] files = studentFiles.listFiles(); //存储所有学生信息 ArrayList<Student> students = new ArrayList<>(); for (File file : files) { //获取评分处理后的学生信息 Student student = disposeTopic(file.getAbsolutePath()); students.add(student); } //返回全部学生集合 return students; } }
import java.util.ArrayList; public class Student { //学生姓名 private String name; //总分 private double score; //试题信息 private ArrayList<Topic> topics; public Student() { } public Student(String name, double score, ArrayList<Topic> topics) { this.name = name; this.score = score; this.topics = topics; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getScore() { return score; } public void setScore(double score) { this.score = score; } public ArrayList<Topic> getTopics() { return topics; } public void setTopics(ArrayList<Topic> topics) { this.topics = topics; } }
public class Topic { //题目编号 private int topicNumber; //正确选项 private String correctOptions; //你的选项 private String yourOptions; //得分 private double topicScore; public Topic() { } public Topic(int topicNumber, String correctOptions, String yourOptions, double topicScore) { this.topicNumber = topicNumber; this.correctOptions = correctOptions; this.yourOptions = yourOptions; this.topicScore = topicScore; } public int getTopicNumber() { return topicNumber; } public void setTopicNumber(int topicNumber) { this.topicNumber = topicNumber; } public String getCorrectOptions() { return correctOptions; } public void setCorrectOptions(String correctOptions) { this.correctOptions = correctOptions; } public String getYourOptions() { return yourOptions; } public void setYourOptions(String yourOptions) { this.yourOptions = yourOptions; } public double getTopicScore() { return topicScore; } public void setTopicScore(double topicScore) { this.topicScore = topicScore; } @Override public String toString() { return "题目【"+topicNumber+"】 正确选项【"+correctOptions+"】 你的选项【"+yourOptions+"】 得分【"+topicScore+"】"; } }
运行结果:
Java 学生评分系统(IO流基础)
于 2022-04-18 22:55:44 首次发布