考试系统随机抽题

在我只做考试系统时,出现了难题就是如何进行随机抽题,经过仔细考虑研究出的代码,给大家分享下:

1.首先在js中发送请求,并传入参数科目和抽题数量

selurl3 = "page/choiceInfoAction!getChoicebyCourse?ids="+coursename+"&sum="+operationId1.list[0].p_select_qno;
jQueryEasyUI.ajaxCRUD(selurl3,"#medicalEquipmentGrid","",function(operationId2) {
for(var i=1;i<=operationId2.list.size();i++){
document.getElementById("qtitle"+i).innerHTML=$.trim(operationId2.list[i-1].q_title);
document.getElementById("qopta"+i).innerHTML=$.trim(operationId2.list[i-1].q_opt_a);
document.getElementById("qoptb"+i).innerHTML=$.trim(operationId2.list[i-1].q_opt_b);
document.getElementById("qoptc"+i).innerHTML=$.trim(operationId2.list[i-1].q_opt_c);
document.getElementById("qoptd"+i).innerHTML=$.trim(operationId2.list[i-1].q_opt_d);
}

2.在后台获取数据,进行抽题,先从数据库中抽题所有该科目的题,然后将结果list顺序打乱,再从中选择一定数量的元素付给另一个list,返回结果

String sid = features.getString("course", "0");
int total=features.getInt32("sum", 0);
List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
List<Map<String, Object>> list1 = new ArrayList<Map<String, Object>>();
Map<String, List<Map<String, Object>>> map = new HashMap<String, List<Map<String, Object>>>();
try{
     String insertSql = "select  * from question t where    t.cou_name=?";
     Object[] arg = {sid};
     int[] argType = {java.sql.Types.VARCHAR};
     list = getJdbcTemplate().queryForList(insertSql,arg,argType);
     Collections.shuffle(list);
     for(int i=0;i<total;i++){
     list1.add(list.get(i));
}
     map.put("list", list1);
} catch (DataAccessException ex) {
     Log.error(ex);
     ex.printStackTrace();
     throw new DaoException(ex);
} 
return map;

希望能对大家有用!!!!

 

posted @ 2013-04-17 10:55  疯情万种  阅读(313)  评论(0)    收藏  举报