java实现随机抽取试题组成试卷
生活随笔
收集整理的這篇文章主要介紹了
java实现随机抽取试题组成试卷
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
| ? @PostMapping("/createPaper") public Result createPaper(){try {TPaper tPaper = new TPaper();Map<String, Object> singlecolumnMap = new HashMap<>();singlecolumnMap.put("data_state","Normal");//獲取所有的單選題singlecolumnMap.put("type","單選題");List<TQuestion> singleList = questionMapper.selectByMap(singlecolumnMap);if(singleList.size()<20){return Result.error(903,"題庫中的單選題不足10道,請添加單選題到數據庫");}//獲取多選題singlecolumnMap.put("type","多選題");List<TQuestion> moreleList = questionMapper.selectByMap(singlecolumnMap);if(moreleList.size()<10){return Result.error(903,"題庫中的多選題不足10道,請添加多選題到數據庫");}//獲取判斷題singlecolumnMap.put("type","判斷題");List<TQuestion> judgeList = questionMapper.selectByMap(singlecolumnMap);if(judgeList.size()<5){return Result.error(903,"題庫中的判斷題不足5道,請添加判斷題到數據庫");}//獲取填空題singlecolumnMap.put("type","填空題");List<TQuestion> tkList = questionMapper.selectByMap(singlecolumnMap);if(tkList.size()<5){return Result.error(903,"題庫中的填空題不足5道,請添加填空題到數據庫");}//抽取單選題 填空5道,單選20,多選10,判斷5List<TQuestion> singleQuestions = getRandom(singleList,20);//抽取多選題List<TQuestion> moreQuestions = getRandom(moreleList,10);//抽取判斷題List<TQuestion> judgeQuestions = getRandom(judgeList,5);//抽取填空題List<TQuestion> tkQuestions = getRandom(tkList,5);String time = TimeUtils.dateToString(new Date());String num = "EM"+time+(int) ((Math.random() * 9 + 1) * 100000);String randomString = getRandomString(6);tPaper.setCreateId(TokenUtil.getUserId());tPaper.setCreateTime(new Date()); //試卷編號tPaper.setPaperNum(num); //試卷密碼tPaper.setPaperPwd(randomString);int create = paperService.createPaper(tPaper, singleQuestions, moreQuestions, judgeQuestions, tkQuestions);if (create<=0){return Result.error(902,"生成試卷失敗");}return Result.ok("生成試卷成功");}catch (Exception e){return Result.error(901,e.getMessage());} } //length用戶要求產生字符串的長度 public static String getRandomString(int length){String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";Random random=new Random();StringBuffer sb=new StringBuffer();for(int i=0;i<length;i++){int number=random.nextInt(62);sb.append(str.charAt(number));}return sb.toString(); } //抽取試題private List<TQuestion> getRandom(List<TQuestion> questionList,int n){List backList = null;backList = new ArrayList<TQuestion>();Random random = new Random();int backSum = 0;if (questionList.size() >= n) {backSum = n;}else {backSum = questionList.size();}for (int i = 0; i < backSum; i++) { // 隨機數的范圍為0-list.size()-1int target = random.nextInt(questionList.size());backList.add(questionList.get(target));questionList.remove(target);}return backList;} |
?
總結
以上是生活随笔為你收集整理的java实现随机抽取试题组成试卷的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java获取随机正整数
- 下一篇: Mybatis实现多表关联多条件查询