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,判断5
        List<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()-1
            int target = random.nextInt(questionList.size());
            backList.add(questionList.get(target));
            questionList.remove(target);
        }
        return backList;
    }

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

iamlzjoco

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值