Java 答题活动随机题目随机答案

//设置返回答题配置和派奖配置
            AnswerInfoDTO answerDTO = new AnswerInfoDTO();
            McCampaignAnswerModel answerModel = answerRepository.findBySchemaId(schemaId).orElseThrow(ErrorCode.NO_RECORD::toException);
            BeanUtils.copyProperties(answerModel, answerDTO);

            //设置返回题目
            List<McCampaignQuestionModel> questions = new ArrayList<>();
            //解析题库id集合
            if (answerModel.getQuestionForm().equals("2")) {
                Arrays.stream(StringUtils.split(answerModel.getQuestionIds(), "\\|")).mapToInt(Integer::valueOf).forEach(id -> {
                    //如果存在该题目,就添加到返回列表中
                    questionRepository.findById(id).ifPresent(questions::add);
                });
            }
            else
            {
                String[] strarr = answerModel.getQuestionIds().split("\\|");//拆分成字符串数组
                Random random = new Random();//随机对象
                List<String> list = new ArrayList<>();
                int i = 0;
                while (i < answerModel.getTitleNum()) {
                    int num = random.nextInt(strarr.length);
                    if (!list.contains(strarr[num])) {
                        list.add(strarr[num]);
                        i++;
                    }
                }
                for (int j = 0; j < list.size(); j++) {
                    int inputquestionid =Integer.parseInt(list.get(j));
                    McCampaignQuestionModel mcquestion= questionRepository.findById(inputquestionid).orElseThrow(ErrorCode.NO_RECORD::toException);
                    questions.add(mcquestion);
                }
                int n = random.nextInt(strarr.length);//随机一个整数
            }
            List<AnswerQuestionDTO> answerPacks = new ArrayList<>();
            questions.forEach(question -> {
                String[] str={question.getCorrectAnswer(),question.getWrongAnswer1(),question.getWrongAnswer2(),question.getWrongAnswer3(),question.getWrongAnswer4(),question.getWrongAnswer5()};
                int k = 0;
                Random randomques = new Random();//随机对象
                List<String> listRandom = new ArrayList<String>();
                List<Integer> intRandom = new ArrayList<Integer>();
                while (k < str.length) {
                    int numques = randomques.nextInt(str.length);
                    if (str[numques] !=null) {
                        if (!listRandom.contains(str[numques])) {
                            listRandom.add(str[numques]);
                            k++;
                        }
                    }
                    else
                    {
                        if (!intRandom.contains(numques)) {
                            intRandom.add(numques);
                            listRandom.add(str[numques]);
                            k++;
                        }
                    }
                }
                AnswerPackDTO answerPackDTO = new AnswerPackDTO();
                AnswerPackDTO answerPackDTO1 = new AnswerPackDTO();
                AnswerPackDTO answerPackDTO2 = new AnswerPackDTO();
                AnswerPackDTO answerPackDTO3 = new AnswerPackDTO();
                AnswerPackDTO answerPackDTO4 = new AnswerPackDTO();
                AnswerPackDTO answerPackDTO5 = new AnswerPackDTO();
                List<AnswerPackDTO> listanswers = new ArrayList<>();
                answerPackDTO.setValue(listRandom.get(0));
                answerPackDTO1.setValue(listRandom.get(1));
                answerPackDTO2.setValue(listRandom.get(2));
                answerPackDTO3.setValue(listRandom.get(3));
                answerPackDTO4.setValue(listRandom.get(4));
                answerPackDTO5.setValue(listRandom.get(5));
                listanswers.add(answerPackDTO);
                listanswers.add(answerPackDTO1);
                listanswers.add(answerPackDTO2);
                listanswers.add(answerPackDTO3);
                listanswers.add(answerPackDTO4);
                listanswers.add(answerPackDTO5);
                AnswerQuestionDTO answerQuestionDTO =new AnswerQuestionDTO();
                answerQuestionDTO.setAnswers(listanswers);
                answerQuestionDTO.setCorrectAnswer(question.getCorrectAnswer());
                answerQuestionDTO.setTitle(question.getTitle());
                answerQuestionDTO.setAnswerNum(question.getAnswerNum());
                answerQuestionDTO.setId(question.getId());
                answerQuestionDTO.setQesImg(question.getQesImg());
                answerQuestionDTO.setType(question.getType());
                answerPacks.add(answerQuestionDTO);
            });
            answerDTO.setQuestionsPack(answerPacks);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值