黑马程序员技术交流社区

标题: 新手练习题求大神指正(2) [打印本页]

作者: luchatex    时间: 2016-9-22 23:32
标题: 新手练习题求大神指正(2)
[Java] 纯文本查看 复制代码
package test2;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;

/*
* 基础:
业务需求:实现学员成绩自动判卷。
1、学员提交以自己姓名命名的TXT文档,将学员答案放到指定文件夹。
2、学员文档内容格式要求, 题号 = 答案
例如: 1 = abc
      2 = EDDFd
3、正确答案保存到 answer.properties 保存格式为 题号 = 答案选项 ,
例如:
1 = BDDF
2 = Ace

4、每题对应分数保存到 score.properties,保存格式为 题号 = 分值 例如
1 = 20
2 = 10
3 = 5

5、运行程序,读取存放学员成绩的文件夹,给每个学员判卷,最后输出一个记录全部学员成绩的TXT文本。
6、总成绩格式要求:
曹操 = 55.0
刘备 = 60.0
孙权 = 100.0
袁绍 = 30.0
诸葛 = 100.0

7、判卷要求:根据答案匹配度给分,例如:第一题 10分,答案是 ABC,如果学员答案是 abd,则答对2项,分值 = 10 / 3 * 2
*/
public class Test {
        public static void main(String[] args) throws IOException, IOException {
                //定义一个学生最终成绩集合
                FileWriter fwp = new FileWriter("d:\\bbb\\全部学员成绩.txt",true);
                BufferedWriter bw = new BufferedWriter(fwp);
                //Properties ppp = new Properties();
                //遍历文件夹
                File file = new File(".");
                File[] listFiles = file.listFiles(new FilenameFilter() {
                       
                        @Override
                        public boolean accept(File dir, String name) {
                                return name.endsWith(".txt");
                        }
                });
                for (File f : listFiles) {
                        //System.out.println(f);
                        FileReader fr = new FileReader(f);
                        //定义三个集合
                        Properties stuProp= new Properties();;
                        Properties answerProp = new Properties();
                        Properties scoreProp = new Properties();
                        //读取三个txt文件
                        stuProp.load(fr);
                        answerProp.load(new FileReader("answer.properties"));
                        scoreProp.load(new FileReader("score.properties"));
                        //
                        //调用方法,去掉key和value的空格
                        trim(stuProp);
                        trim(answerProp);
                        trim(scoreProp);
                        //System.out.println(stuProp);
                        //System.out.println(answerProp);
                        //System.out.println(scoreProp);
                        //1.先把标准答案展开,拿学生成绩做比较
                        Set<String> ap = answerProp.stringPropertyNames();
                        double scoreTotal = 0;
                        for (String key : ap) {
                                String ans = answerProp.getProperty(key);
                                String stu = stuProp.getProperty(key);
                                Double sco = Double.parseDouble(scoreProp.getProperty(key)) ;
                                //System.out.println(stu);
                                for (int i = 0; i < ans.length(); ) {
                                        int count = 0;
                                        String sub1 = ans.substring(i, i+1);
                                        String sub2 = stu.substring(i, i+1);
                                        if(sub2.equals(sub1)){
                                                count++;
                                        }
                                        scoreTotal += sco/ans.length()*count;
                                        //System.out.println(sub2);
                                        i++;
                                        //System.out.println(count);
                                }
                        }
                        int last = f.getName().lastIndexOf(".");
                        String key = f.getName().substring(0, last).concat(" = ");
                        //System.out.println(key);
                        String value = String.valueOf(Math.round(scoreTotal));
                        //System.out.println(value);
                        //存学生成绩
                        //ppp.setProperty("hehe",value);
                        bw.write((key.concat(value)));
                        bw.newLine();
                }
                bw.close();
                fwp.close();
        }
        private static void trim(Properties prop) {
                Set<String> p = prop.stringPropertyNames();
                for (String key : p) {
                        key = key.trim();
                        //System.out.println(key);
                        String value = prop.getProperty(key).trim().toUpperCase();
                        prop.setProperty(key, value);
                        //System.out.println(value);
                }
        }
}





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2