黑马程序员技术交流社区

标题: 选择题自动评分 [打印本页]

作者: jiubaK    时间: 2016-9-1 01:35
标题: 选择题自动评分
实现选择题目自动评分,后期实际项目可能会用到。
暂且如此,后面发现bug再行更改。睡了先...
[Java] 纯文本查看 复制代码
import java.io.*;
import java.util.*;
/**
Checker类
用于自动批改选择题,获取得分。
答案模板格式:
1-5:A,A,A,A,A
6-10:A,A,A,A,A
......
可以有多选,默认属性适用于40题卷(30单选,每题2分;10多选,每题4分)
具体分值可以自行设定,题目的数目完全依赖于答案模板。
*/
class Checker {

        //每个题目的分值,默认单选2分,多选4分
        private int score1 = 2, score2 = 4;

        //答案模板的文件路径
        private String answerFileName;

        //构造函数
        Checker(){}
        Checker(String answerFileName){
                this.answerFileName = answerFileName;
        }
        //get/set方法
        public void setScore1(int score1){
                this.score1 = score1;
        }
        public int getScore1(){
                return score1;
        }
        public void setScore2(int score2){
                this.score2 = score2;
        }
        public int getScore2(){
                return score2;
        }
        public void setAnswer(String answerFileName){
                this.answerFileName = answerFileName;
        }
        public String getAnswer(){
                return answerFileName;
        }
        /*
        通过输入流获取要评分的答案,并与答案模板比对,返回得分
        */
        public int check(InputStream is){
                BufferedReader base = null;
                BufferedReader stu = null;
                int score = 0;
                try {
                        base = new BufferedReader( new FileReader(answerFileName) );
                        stu = new BufferedReader( new InputStreamReader( is) );
                        String[] temp1,temp2;
                        for (String baseLine,stuLine ; (baseLine = base.readLine())!=null && (stuLine = stu.readLine())!=null ; ) {
                                temp1 = baseLine.split(":");
                                temp2 = stuLine.split(":");
                                if ( temp1.length != 2 || temp2.length!=2 ) {
                                        continue;
                                }
                                temp1 = temp1[1].split(",");
                                temp2 = temp2[1].split(",");
                                int min = temp1.length;
                                if ( min > temp2.length ) {
                                        min = temp2.length;
                                }
                                byte[] bts;
                                for ( int i = 0 ; i < min ; i++ ) {
                                        baseLine = temp1.trim().toUpperCase();
                                        bts = baseLine.getBytes();
                                        Arrays.sort(bts);
                                        baseLine = new String(bts);
                                        stuLine = temp2.trim().toUpperCase();
                                        bts = stuLine.getBytes();
                                        Arrays.sort(bts);
                                        stuLine = new String(bts);

                                        if ( baseLine.equals(stuLine) ) {
                                                if ( baseLine.length() == 1 ) {
                                                        score += score1;
                                                } else {
                                                        score += score2;
                                                }
                                        } else {
                                                boolean f = true;
                                                for ( int j = 0 ; j < stuLine.length(); j++ ) {
                                                        if ( !baseLine.contains(stuLine.charAt(j)+"") ) {
                                                                f = false;
                                                                break;
                                                        }
                                                }
                                                if ( f ) {
                                                        score += score1;
                                                }
                                        }
                                }
                        }
                } catch(FileNotFoundException e){
                        e.printStackTrace();
                } catch(IOException e){
                        e.printStackTrace();
                } finally {
                        try {
                                if ( base != null ) {base.close();}
                                if ( stu != null ) {stu.close();}
                        } catch (IOException e ) {
                                e.printStackTrace();
                        }
                }
                return score;
        }
}






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