JSP学习笔记(一百二十六):多选题选项选对给全分,选对一部分给一半分

今天在开发考试系统时,有这么一个需求:多选题选项选对给全分,选一部分给一半分

下面是代码:

    public static float getScoreRate(String topicKey,String topicAnswer) {
        float returnValue = 0;
        if(topicAnswer.equals(topicKey)) {
            returnValue = 1f;
        } else {
            boolean hitFlag = true;
            for (int i = 0; i < topicAnswer.length(); i++) {
                if(topicKey.indexOf(topicAnswer.charAt(i))==-1) {
                    hitFlag = false;
                    break;
                }
            }
            if(hitFlag) returnValue = 0.5f;
        }
        return returnValue;
    }

    public static void main(String[] args) {
        String topicKey = "ABD";    //标准答案
        float score = 5f;    //试题分数
        
        System.out.println("ABC" + "____" + score * getScoreRate(topicKey,"ABC"));
        System.out.println("A" + "____" + score * getScoreRate(topicKey,"A"));
        System.out.println("AB" + "____" + score * getScoreRate(topicKey,"AB"));
        System.out.println("ABCD" + "____" + score * getScoreRate(topicKey,"ABCD"));
        System.out.println("ABD" + "____" + score * getScoreRate(topicKey,"ABD"));
    }

输出:

ABC____0.0
A____2.5
AB____2.5
ABCD____0.0
ABD____5.0

 

posted @ 2014-07-09 14:32  魔豆  阅读(539)  评论(0编辑  收藏  举报