java实现题目以及选项乱序的方法实例
前言
在现实生活中,考试是我们的必经之路,但是线上考试总有一部分人抄写答案,为了防止抄写,将题目和答案打乱,防止抄袭。本文实现通过map轮询时将选项乱序,最终答案也变化。
选择题类ChoiceQuestion
importjava.util.Map;
/**
*单选题
*/
publicclassChoiceQuestion{
privateStringname;//题目
privateMapoption;//选项;A、B、C、D
privateStringkey;//答案;B
publicChoiceQuestion(){
}
publicChoiceQuestion(Stringname,Mapoption,Stringkey){
this.name=name;
this.option=option;
this.key=key;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicMapgetOption(){
returnoption;
}
publicvoidsetOption(Mapoption){
this.option=option;
}
publicStringgetKey(){
returnkey;
}
publicvoidsetKey(Stringkey){
this.key=key;
}
}
    
处理后选项答案临时对象Topic
publicclassTopic{
privateMapoption;//选项;A、B、C、D
privateStringkey;//答案;B
publicTopic(){
}
publicTopic(Mapoption,Stringkey){
this.option=option;
this.key=key;
}
publicMapgetOption(){
returnoption;
}
publicvoidsetOption(Mapoption){
this.option=option;
}
publicStringgetKey(){
returnkey;
}
publicvoidsetKey(Stringkey){
this.key=key;
}
}
    
测试类
/**
*@date2021/3/316:54
*@version:1.0
*/
publicclassQuestionTest{
/**
*输出对于考生的题目
*@paramcandidate
*@paramnumber
*/
publicstaticvoidoutput(Stringcandidate,Stringnumber){
Mapmap01=newHashMap();
map01.put("A","JAVA2EE");
map01.put("B","JAVA2Card");
map01.put("C","JAVA2ME");
map01.put("D","JAVA2HE");
map01.put("E","JAVA2SE");
Mapmap02=newHashMap();
map02.put("A","JAVA程序的main方法必须写在类里面");
map02.put("B","JAVA程序中可以有多个main方法");
map02.put("C","JAVA程序中类名必须与文件名一样");
map02.put("D","JAVA程序的main方法中如果只有一条语句,可以不用{}(大括号)括起来");
Mapmap03=newHashMap();
map03.put("A","变量由字母、下划线、数字、$符号随意组成;");
map03.put("B","变量不能以数字作为开头;");
map03.put("C","A和a在java中是同一个变量;");
map03.put("D","不同类型的变量,可以起相同的名字;");
Mapmap04=newHashMap();
map04.put("A","STRING");
map04.put("B","x3x;");
map04.put("C","void");
map04.put("D","de$f");
Mapmap05=newHashMap();
map05.put("A","31");
map05.put("B","0");
map05.put("C","1");
map05.put("D","2");
ArrayListchoiceQuestionList=newArrayList();
choiceQuestionList.add(newChoiceQuestion("JAVA所定义的版本中不包括",map01,"D"));
choiceQuestionList.add(newChoiceQuestion("下列说法正确的是",map02,"A"));
choiceQuestionList.add(newChoiceQuestion("以下()不是合法的标识符",map04,"C"));
choiceQuestionList.add(newChoiceQuestion("表达式(11+3*8)/4%3的值是",map05,"D"));
choiceQuestionList.add(newChoiceQuestion("变量命名规范说法正确的是",map03,"B"));
//题目顺序打乱
Collections.shuffle(choiceQuestionList);
//选项-答案乱序
for(ChoiceQuestionquestion:choiceQuestionList){
Topicrandom=random(question.getOption(),question.getKey());
question.setOption(random.getOption());
question.setKey(random.getKey());
}
//输出打印
System.out.println(toString(choiceQuestionList,candidate,number));
}
/**
*打乱选项和答案
*@paramoption
*@paramkey
*@return
*/
publicstaticTopicrandom(Mapoption,Stringkey){
SetkeySet=option.keySet();
ArrayListkeyList=newArrayList(keySet);
Collections.shuffle(keyList);
HashMapoptionNew=newHashMap();
intidx=0;
StringkeyNew="";
for(Stringnext:keySet){
StringrandomKey=keyList.get(idx++);
if(key.equals(next)){
keyNew=randomKey;
}
optionNew.put(randomKey,option.get(next));
}
returnnewTopic(optionNew,keyNew);
}
publicstaticStringtoString(ArrayListchoiceQuestionList,Stringcandidate,Stringnumber){
StringBuilderdetail=newStringBuilder("考生:"+candidate+"\r\n"+
"考号:"+number+"\r\n"+
"--------------------------------------------\r\n"+
"一、选择题"+"\r\n\n");
for(intidx=0;idxoption=choiceQuestionList.get(idx).getOption();
for(Stringkey:option.keySet()){
detail.append(key).append(":").append(option.get(key)).append("\r\n");;
}
detail.append("答案:").append(choiceQuestionList.get(idx).getKey()).append("\r\n\n");
}
returndetail.toString();
}
publicstaticvoidmain(String[]args){
output("花花","1000001921032");
output("豆豆","1000001921051");
output("大宝","1000001921987");
}
}
                    
输出结果
考生:花花
考号:1000001921032
--------------------------------------------
一、选择题第1题:JAVA所定义的版本中不包括
A:JAVA2HE
B:JAVA2ME
C:JAVA2SE
D:JAVA2Card
E:JAVA2EE
答案:A第2题:下列说法正确的是
A:JAVA程序中可以有多个main方法
B:JAVA程序中类名必须与文件名一样
C:JAVA程序的main方法必须写在类里面
D:JAVA程序的main方法中如果只有一条语句,可以不用{}(大括号)括起来
答案:C第3题:以下()不是合法的标识符
A:x3x;
B:void
C:de$f
D:STRING
答案:B第4题:表达式(11+3*8)/4%3的值是
A:0
B:31
C:1
D:2
答案:D第5题:变量命名规范说法正确的是
A:变量不能以数字作为开头;
B:A和a在java中是同一个变量;
C:变量由字母、下划线、数字、$符号随意组成;
D:不同类型的变量,可以起相同的名字;
答案:A
考生:豆豆
考号:1000001921051
--------------------------------------------
一、选择题第1题:JAVA所定义的版本中不包括
A:JAVA2Card
B:JAVA2EE
C:JAVA2SE
D:JAVA2HE
E:JAVA2ME
答案:D第2题:下列说法正确的是
A:JAVA程序的main方法必须写在类里面
B:JAVA程序的main方法中如果只有一条语句,可以不用{}(大括号)括起来
C:JAVA程序中可以有多个main方法
D:JAVA程序中类名必须与文件名一样
答案:A第3题:以下()不是合法的标识符
A:void
B:x3x;
C:de$f
D:STRING
答案:A第4题:表达式(11+3*8)/4%3的值是
A:31
B:2
C:0
D:1
答案:B第5题:变量命名规范说法正确的是
A:A和a在java中是同一个变量;
B:变量由字母、下划线、数字、$符号随意组成;
C:变量不能以数字作为开头;
D:不同类型的变量,可以起相同的名字;
答案:C
考生:大宝
考号:1000001921987
--------------------------------------------
一、选择题第1题:JAVA所定义的版本中不包括
A:JAVA2ME
B:JAVA2Card
C:JAVA2SE
D:JAVA2HE
E:JAVA2EE
答案:D第2题:下列说法正确的是
A:JAVA程序中类名必须与文件名一样
B:JAVA程序的main方法必须写在类里面
C:JAVA程序中可以有多个main方法
D:JAVA程序的main方法中如果只有一条语句,可以不用{}(大括号)括起来
答案:B第3题:以下()不是合法的标识符
A:STRING
B:de$f
C:void
D:x3x;
答案:C第4题:表达式(11+3*8)/4%3的值是
A:2
B:0
C:1
D:31
答案:A第5题:变量命名规范说法正确的是
A:不同类型的变量,可以起相同的名字;
B:变量不能以数字作为开头;
C:变量由字母、下划线、数字、$符号随意组成;
D:A和a在java中是同一个变量;
答案:B
本文参考bugstack⾍洞栈作者设计模式一书原型模式提取选项答案乱序逻辑修改
总结
到此这篇关于java实现题目以及选项乱序的文章就介绍到这了,更多相关java题目选项乱序内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。