考试系统——考卷设计思路

表的设计。管理员是/login2.jsp登录。

根据paperId获取试卷题目,然后按题目类型放入不同的集合,便于再页面生成多选或者单选题。

    public String getDetailPaper()throws Exception{
        paper=paperDao.getPaper(paperId);
        Set<Question> questionList=paper.getQuestions();
        Iterator<Question> it=questionList.iterator();
        while(it.hasNext()){
            Question q=it.next();
            if("1".equals(q.getType())){//按题目类型放入不同的集合
                squestionList.add(q);
            }else{
                mquestionList.add(q);
            }
        }
        squestionList=this.getRandowQuestion(squestionList, 3);//随机选取题目
        mquestionList=this.getRandowQuestion(mquestionList, 2);
        mainPage="exam/paper.jsp";
        return SUCCESS;
    }

    /*随机选取题目,总共选N题,用for循环N次,如果题目重复,N-1补回一次*/
    private List<Question> getRandowQuestion(List<Question> questionList,int num){
        List<Question> resultList=new ArrayList<Question>();
        Random random=new Random();
        if(num>0){
            for(int i=1;i<=num;i++){
                int n=random.nextInt(questionList.size());
                Question q=questionList.get(n);
                if(resultList.contains(q)){
                    i--;
                }else{
                    resultList.add(q);
                }
            }
        }
        return resultList;
    }
View Code
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
<script type="text/javascript">
    var examTime=20*60;
    var useTime=0,remainTime=examTime;
    
    function showCount(){
        
        if(remainTime==0){
            document.getElementById("myForm").submit();
        }
        
        useTime+=1;
        remainTime-=1;
        
        var hourU=Math.floor(useTime/3600);
        var minuteU=Math.floor((useTime-hourU*3600)/60);
        var secondU=Math.floor(useTime-hourU*3600-minuteU*60);
        document.getElementById("useTime").innerHTML=format(hourU)+":"+format(minuteU)+":"+format(secondU);
        
        var hourR=Math.floor(remainTime/3600);
        var minuteR=Math.floor((remainTime-hourR*3600)/60);
        var secondR=Math.floor(remainTime-hourR*3600-minuteR*60);
        document.getElementById("remainTime").innerHTML=format(hourR)+":"+format(minuteR)+":"+format(secondR);
    }
    
    function format(timeNumber){
        if(timeNumber<10){
            return "0"+timeNumber;
        }else{
            return timeNumber;
        }
    }
    
    window.setInterval("showCount()", 1000);
</script>
</head>
<body>
<div class="data_list">
    <div class="data_info">
        <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;考试时间:<strong>20分钟</strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        计时:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font id="useTime" style="font-weight: bold;"></font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        剩余时间:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font id="remainTime" style="font-weight: bold;"></font></p>
        <hr/>
        <p class="examTitle">${paper.paperName}&nbsp;&nbsp;考试卷</p>
        <p class="examScoreInfo">(&nbsp;满分120&nbsp;&nbsp;单选题60分&nbsp;&nbsp;多选题60分&nbsp;)</p>
    </div>
    <div class="data_exam_content">
        <form id="myForm" action="exam!add" method="post">
        <input type="hidden" name="exam.student.id" value="${currentUser.id }"/>
        <input type="hidden" name="exam.paper.id" value="${paper.id }"/>
        <strong><big>一,单选题</big></strong>(每题20分,答错不得分)<br/><br/>
        <c:forEach var="s" items="${squestionList}" varStatus="status">
            <strong>[&nbsp;${status.index+1 }&nbsp;]&nbsp;${s.subject }</strong><br/><br/>
            <label class="radio">
                <input type="radio" name="id-r-${s.id }" value="A"/>
                ${s.optionA }
            </label>
            <label class="radio">
                <input type="radio" name="id-r-${s.id }" value="B"/>
                ${s.optionB }
            </label>
            <label class="radio">
                <input type="radio" name="id-r-${s.id }" value="C"/>
                ${s.optionC }
            </label>
            <label class="radio">
                <input type="radio" name="id-r-${s.id }" value="D"/>
                ${s.optionD }
            </label>
            <br/>
        </c:forEach>
        <br/>
        <strong><big>一,多选题</big></strong>(每题30分,答错不得分)<br/><br/>
        <c:forEach var="m" items="${mquestionList}" varStatus="status">
            <strong>[&nbsp;${status.index+1 }&nbsp;]&nbsp;${m.subject }</strong><br/><br/>
            <label class="checkbox">
                <input type="checkbox" name="id-c-${m.id }" value="A"/>
                ${m.optionA }
            </label>
            <label class="checkbox">
                <input type="checkbox" name="id-c-${m.id }" value="B"/>
                ${m.optionB }
            </label>
            <label class="checkbox">
                <input type="checkbox" name="id-c-${m.id }" value="C"/>
                ${m.optionC }
            </label>
            <label class="checkbox">
                <input type="checkbox" name="id-c-${m.id }" value="D"/>
                ${m.optionD }
            </label>
            <br/>
        </c:forEach>
        <button class="btn btn-primary" type="submit">交卷</button>
        </form>
    </div>
</div>
</body>
</html>
View Code

 

试卷提交,自动计算得分。单选题的<input type="radio" name="id-r-${s.id }" value="A"/>, 多选题的<input type="checkbox" name="id-c-${m.id }" value="A"/>

由于请求参数比较多,用Map<String, String[]> keyMap=request.getParameterMap()比较方便。

public String add()throws Exception{
        Map<String, String[]> keyMap=request.getParameterMap();
        Iterator<Entry<String,String[]>> it2=keyMap.entrySet().iterator();
        int totalScore=0;
        int singleScore=0;
        int moreScore=0;
        while(it2.hasNext()){
            Entry<String,String[]> entry=it2.next();
            String keyStr=entry.getKey();
            String values[]=entry.getValue();
            String key;
            String value="";
            if(keyStr.equals("exam.student.id")||keyStr.equals("exam.paper.id")){
                continue;
            }
            if(keyStr.split("-")[1].equals("r")){ // 单选题目
                key=keyStr.split("-")[2];
                value=values[0];
                singleScore+=this.calScore(key, value, "1");
            }else{  // 多选
                key=keyStr.split("-")[2];
                for(String s:values){
                    value+=s+",";
                }
                value=value.substring(0, value.length()-1);//去掉最后一个,
                moreScore+=this.calScore(key, value, "2");
            }
        }
        totalScore=singleScore+moreScore;
        exam.setSingleScore(singleScore);
        exam.setMoreScore(moreScore);
        exam.setScore(totalScore);
        exam.setExamDate(new Date());
        
        examDao.saveExam(exam);
        mainPage="exam/examResult.jsp";
        return SUCCESS;
    }

    /*不同题型使用不同的计分*/
    private int calScore(String questionId,String userAnswer,String type)throws Exception{
        Question question=questionDao.getQuestion(questionId);
        if(userAnswer.equals(question.getAnswer())){
            if("1".equals(type)){
                return 20;
            }else{
                return 30;
            }
        }else{
            return 0;
        }
    }
View Code

 

添加试卷信息比较简单,按如下表单填入数据,直接插入到数据表t_question。

 

posted @ 2017-04-10 14:56  SKYisLimit  阅读(433)  评论(0)    收藏  举报