主入口类
package MONA;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
showFun();
}
public static void showFun(){
//可以一直进行操作
while (true){
System.out.println("请选择功能");
System.out.println("1.评分单个学生");
System.out.println("2.评分全部学生");
System.out.println("3.退出");
Scanner sc = new Scanner(System.in);
switch (sc.nextInt()){
case 1:
System.out.println("1");
break;
case 2:
System.out.println("2");
break;
case 3:
System.out.println("感谢使用");
System.exit(0);
break;
}
}
}
}
判断文件
private static void singleStudent() {
//如果路径不正确,提示继续输入
while (true) {
System.out.println("请输入正确答案路径(文件)");
//正确答案路径
String templatePath = sc.next();
//判断路径是否正确
File file = new File(templatePath);
if (!file.isFile()) {
System.out.println("路径不正确,必须是文件");
} else {
break;
}
}
while (true) {
System.out.println("请输入学生答案路径(文件)");
String stuPath = sc.next();
if (!new File(stuPath).isFile()) {
System.out.println("路径不正确,必须是文件");
} else {
break;
}
}
System.out.println("之后的代码");
}
解答信息思路:
将解析后的数据存入Map集合,Map<Integer,String> 1.读取文件,使用IO流按行读取 1-5:C,D,D,D,C 2.使用":"进行分割 split(":") [1-5] [C,D,D,D,C] 3.使用"-"对数组的第0个元素进行分割(解析题目编号) [1] [5] 4.使用","对数组的第0个元素进行分割(解析答案) [c] [D] [D] [D] [C] 5.遍历答案的数组,存入Map集合 1 A 2 B key:第0个元素[0] + 遍历答案的次数 6.答案比较 正确答案1:ABC 学生答案1:CBA 两个答案的结果是一样的,使用equals进行比较会是false 对答案进行排序,比较排序之后的数据
###存储单个学生的信息 存储考试结果信息,使用自定义的类 学生类:Student 学生姓名 总分 试题信息(每一道题的答题信息) 题目信息类 Topic 题目编号 正确选项 你的选项 得分 ###创建文件夹问题 File file = new File("single完成/李四.txt"); single完成 文件夹不存在 李四.txt 文件不存在 需求:创建文件时,如果文件夹不存在,那么主动创建文件夹和文件 解决:获取父File对象
完整代码
测试类
package MONA;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.*;
/**
* 主入口类
* 1.文件夹不要有空格
* 2.文件路径尽量不要有中文
*/
public class Demo {
//全局SC
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) throws Exception {
showFun();
}
public static void showFun() throws Exception {
//可以一直进行操作
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");
singleStudent();
break;
case 2:
allStudent();
System.out.println("2");
break;
case 3:
System.out.println("感谢使用");
System.exit(0);
break;
}
}
}
private static void allStudent() throws Exception {
String templatePath;
while (true) {
System.out.println("请输入正确答案路径(文件)");
//正确答案路径
templatePath = sc.next();
//判断路径是否正确
File file = new File(templatePath);
if (!file.isFile()) {
System.out.println("路径不正确,必须是文件");
} else {
break;
}
}
String stuPath;
while (true) {
System.out.println("请输入学生答案路径(文件夹)");
stuPath = sc.next();
if (!new File(stuPath).isDirectory()) {
System.out.println("路径不正确,必须是文件夹");
} else {
break;
}
}
ArrayList<Student> students = ScoreUtils.startAll(templatePath, stuPath);
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:
//显示所有学生信息
for (Student student : students) {
System.out.println("姓名:"+student.getName()+" 分数:"+student.getScore());
}
break;
case 2:
//对ArrayList<Student>
//自定义排序规则
Collections.sort(students, (o1, o2) -> (int)(o2.getScore() - o1.getScore()));
// Collections.sort(students,(o1,o2)->{
// return 0;
// });
for (Student student : students) {
System.out.println("姓名:"+student.getName()+" 分数:"+student.getScore());
}
break;
case 3:
mostError(students);
break;
case 4:
//输出所有学生信息到文件中
//输出的路径:所有学生
for (Student student : students) {
saveFile("所有学生",student);
}
break;
case 5:
return;
}
}
}
//错题排名
private static void mostError(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.getTopicScoer() == 0){
//错误
if(errorMap.containsKey(topic.getTopicNumber())){
errorMap.put(topic.getTopicNumber(),errorMap.get(topic.getTopicNumber())+1);
}else {
errorMap.put(topic.getTopicNumber(),1);
}
}
}
}
//System.out.println(errorMap);
/**
* map排序:按照值进行排序
* 思路:
* 1.先将map集合转成list集合
* 2.借助Collections.sort();对List进行排序
* 3.将List转回Map集合
* 利用LinkedHashMap数据插入有序的特点,将List集合的数据遍历存入LinkedHashMap中
*/
//Set<Map.Entry<Integer,Integer>> entries = errorMap.entrySet();
//1.
List<Map.Entry<Integer,Integer>> list = new ArrayList<>(errorMap.entrySet());
//2.
Collections.sort(list, new Comparator<Map.Entry<Integer, Integer>>() {
@Override
public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) {
return o2.getValue()-o1.getValue();
}
});
//3.
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));
}
}
private static void singleStudent() throws Exception {
//如果路径不正确,提示继续输入
String templatePath;
while (true) {
System.out.println("请输入正确答案路径(文件)");
//正确答案路径
templatePath = sc.next();
//判断路径是否正确
File file = new File(templatePath);
if (!file.isFile()) {
System.out.println("路径不正确,必须是文件");
} else {
break;
}
}
String stuPath;
while (true) {
System.out.println("请输入学生答案路径(文件)");
stuPath = sc.next();
if (!new File(stuPath).isFile()) {
System.out.println("路径不正确,必须是文件");
} else {
break;
}
}
System.out.println("之后的代码");
//需求:创建文件时,如果文件夹不存在,那么主动创建文件夹和文件
//解决:获取父File对象
File file = null;
File parentFile = file.getParentFile();
if(!parentFile.exists()){
parentFile.mkdirs();
}
if(file.exists()){
file.createNewFile();
}
Student student = ScoreUtils.startSingle(templatePath,stuPath);
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("分数为:"+student.getScore());
break;
case 2:
//将学生的考试信息保存到文件中
saveFile("single完成",student);
break;
case 3:
return;
}
}
}
/**
* 将学生的考试信息保存到文件中
* @param student
*/
private static void saveFile(String path,Student student) throws Exception {
//single 完成/李四.txt
File file = new File(path+"\\"+student.getName()+".txt");
//获取父文件对象
File parentFile = file.getParentFile();
if(!parentFile.exists()){
parentFile.mkdirs();
}
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());
}
}
别的类
package MONA;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class T {
public static void main(String[] args) {
// 实例化一个TreeSet
// TreeSet<Student> stuSet = new TreeSet<>(new MyComparator());
// stuSet.add(new Student("李四", 20));
// stuSet.add(new Student("张三", 22));
// stuSet.add(new Student("王五", 24));
// for (Student stu : stuSet) {
// System.out.println(stu.name + "," + stu.age);
// }
ArrayList<String> name = new ArrayList<>();
name.add("A");
name.add("B");
name.add("C");
name.add("D");
Collections.sort(name);
System.out.println(name);
}
//排序比较器
static class MyComparator implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
// 先按姓名比
int n1 = o1.name.compareTo(o2.name);
// 如果姓名相同,比较年龄
int n2 = (n1 == 0 ? o1.age - o2.age : n1);
return 0;
}
}
//学生类
static class Student {
String name;
int age;
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
}
}
ScoreUtils类
package MONA;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
public class ScoreUtils {
//解析正确答案数据
static HashMap<Integer, String> correctAnswerMap;
/**
* 解析单个学生
*
* @param templatePath 正确答案文件路径
* @param stuPath 学生答案文件路径
* @return 解析后的学生对象
*/
public static Student startSingle(String templatePath, String stuPath) throws Exception {
//解析正确答案数据
correctAnswerMap = parseAnswer(templatePath);
//评分 比较两个map集合
//将评分的结果进行存储
//System.out.println(correctAnswerMap);
//System.out.println(stuAnswerMap);
//评分
return disposeTopic(stuPath);
}
/**
* 处理学生的答题信息
* @param stuPath 学生答案的路径
* @return 处理后的学生对象
*/
public static Student disposeTopic(String stuPath) throws Exception {
//学生答案数据
HashMap<Integer, String> stuAnswerMap = parseAnswer(stuPath);
//通过路径获取学生姓名
File file = new File(stuPath);
String name = file.getName().split("\\.")[0];
Student student = new Student();
//设置学生的姓名
student.setName(name);
//存储答题信息
ArrayList<Topic> topics = new ArrayList<>();
double totalScore = 0;
for (int i = 1; i <= stuAnswerMap.size(); i++) {
//获取正确的答案
String correctAnswer = correctAnswerMap.get(i);
//获取学生的答案
String stuAnswer = stuAnswerMap.get(i);
Topic topic = new Topic();
//设置正确答案信息
topic.setCorrectOptions(correctAnswer);
//学生的选项
topic.setYourOptions(stuAnswer);
//题目编号
topic.setTopicNumber(i);
if (correctAnswer.equals(stuAnswer)) {
//正确
//设置题目分数
topic.setTopicScoer(2.5);
//总分
totalScore += 2.5;
} else {
//漏选 错选不得分
topic.setTopicScoer(0);
}
//添加题目到集合
topics.add(topic);
}
student.setScore(totalScore);
student.setTopics(topics);
//System.out.println(stuAnswerMap);
return student;
}
/**
* 解析答案
* @param answerPath 答案路径
* @return 返回解析后的答案Map集合
*/
public static HashMap<Integer,String> parseAnswer(String answerPath) throws Exception {
//存储解析后的答案数据
HashMap<Integer,String> tempAnswer = new HashMap();
//使用io流读取数据(字符流按行读取)
BufferedReader br = new BufferedReader(new FileReader(answerPath));
//记录一行信息
String line;
while ((line = br.readLine()) !=null){
//System.out.println("读取到的数据:"+line);
//使用:进行分割 split(":")
String[] temp = line.split(":");
//编号
String[] numberArr = temp[0].split("-");
//答案
String[] answerArr = temp[1].split(",");
//题目编号大小
int topicNumberSize = Integer.parseInt(numberArr[0]);
for (int i = 0; i < answerArr.length; i++) {
//获取未排序的答案
String answer = answerArr[i];
//String --> byte[]
byte[] answers = answer.getBytes();
//排序答案数组
Arrays.sort(answers);
//将排序后的数组转成String类型
answer = new String(answer);
//tempAnswer.put<"编号","答案">;
tempAnswer.put(topicNumberSize + i,answer);
}
}
//System.out.println("答案map "+tempAnswer);
return tempAnswer;
}
/**
* 评分全部学生
* @param templatePath 之前答案路径
* @param stuPath 学生答案的文件夹
* @return 返回所有学生信息
*/
public static ArrayList<Student> startAll(String templatePath, String stuPath) throws Exception {
//正确答案
correctAnswerMap = parseAnswer(templatePath);
//学生答案
//获取文件夹中所有文件
File stuFile = new File(stuPath);
File[] files = stuFile.listFiles();
File f = new File(stuPath);
f.getParentFile().getAbsolutePath();
//储存所有学生
//存储所有学生
ArrayList<Student> students = new ArrayList<>();
for (File file : files) {
Student student = disposeTopic(file.getAbsolutePath());
students.add(student);
}
return students;
}
}
topic类
package MONA;
/**
* 题目信息
*/
public class Topic {
private int topicNumber;
//正确选项
private String correctOptions;
//你的选项
private String yourOptions;
//得分
private double topicScoer;
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 getTopicScoer() {
return topicScoer;
}
public void setTopicScoer(double topicScoer) {
this.topicScoer = topicScoer;
}
@Override
public String toString(){
return "题目【"+topicNumber+"】 正确选项【"+correctOptions+"】 你的选项【"+yourOptions+"】 得分【"+topicScoer+"】";
}
}
Student类
package MONA;
import java.util.ArrayList;
/**
* 学生信息类
*/
public class Student {
private String name;
//分数
private double score;
//答题信息
private ArrayList<Topic> 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;
}
}