几天晚上在做那个在线教学,要随机抽取选择题和判断题,实现了,代码贴出来备用
public HashSet querySelectQues(){
try {
//set放结果,无重复
HashSet<Examques> result = new HashSet<Examques>();
//查询出所有选择题(因为所有的题在一起放着)
List<Examques> list=getHibernateTemplate().find("from Examques as examques where examques.examquesStyle = 1");
int maxSize = list.size();
//只有当题的数目符合时在跳出循环
while (true) {
int randNumber = (int) (Math.random() * maxSize + 1) - 1;
result.add(list.get(randNumber));
//将题的数目设成全局的
if (result.size() >= ExamOnlineConstant.selectQuesNum) {
break;
}
}
return result;
}
catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}