java实现题目以及选项乱序的方法实例

 更新时间:2021年03月09日 11:45:01   作者:lixy940  
这篇文章主要给大家介绍了关于java实现题目以及选项乱序的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

前言

在现实生活中,考试是我们的必经之路,但是线上考试总有一部分人抄写答案,为了防止抄写,将题目和答案打乱,防止抄袭。本文实现通过map轮询时将选项乱序,最终答案也变化。

选择题类 ChoiceQuestion

import java.util.Map;

/**
 * 单选题
 */
public class ChoiceQuestion {

 private String name;     // 题目
 private Map<String, String> option; // 选项;A、B、C、D
 private String key;     // 答案;B

 public ChoiceQuestion() {
 }

 public ChoiceQuestion(String name, Map<String, String> option, String key) {
  this.name = name;
  this.option = option;
  this.key = key;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public Map<String, String> getOption() {
  return option;
 }

 public void setOption(Map<String, String> option) {
  this.option = option;
 }

 public String getKey() {
  return key;
 }

 public void setKey(String key) {
  this.key = key;
 }
}

处理后选项答案临时对象 Topic

public class Topic {

 private Map<String, String> option; // 选项;A、B、C、D
 private String key;   // 答案;B

 public Topic() {
 }

 public Topic(Map<String, String> option, String key) {
  this.option = option;
  this.key = key;
 }

 public Map<String, String> getOption() {
  return option;
 }

 public void setOption(Map<String, String> option) {
  this.option = option;
 }

 public String getKey() {
  return key;
 }

 public void setKey(String key) {
  this.key = key;
 }
}

测试类

/**
 * @date 2021/3/3 16:54
 * @version: 1.0
 */
public class QuestionTest {
 /**
  * 输出对于考生的题目
  * @param candidate
  * @param number
  */
 public static void output(String candidate, String number){
  Map<String, String> map01 = new HashMap<String, String>();
  map01.put("A", "JAVA2 EE");
  map01.put("B", "JAVA2 Card");
  map01.put("C", "JAVA2 ME");
  map01.put("D", "JAVA2 HE");
  map01.put("E", "JAVA2 SE");

  Map<String, String> map02 = new HashMap<String, String>();
  map02.put("A", "JAVA程序的main方法必须写在类里面");
  map02.put("B", "JAVA程序中可以有多个main方法");
  map02.put("C", "JAVA程序中类名必须与文件名一样");
  map02.put("D", "JAVA程序的main方法中如果只有一条语句,可以不用{}(大括号)括起来");

  Map<String, String> map03 = new HashMap<String, String>();
  map03.put("A", "变量由字母、下划线、数字、$符号随意组成;");
  map03.put("B", "变量不能以数字作为开头;");
  map03.put("C", "A和a在java中是同一个变量;");
  map03.put("D", "不同类型的变量,可以起相同的名字;");

  Map<String, String> map04 = new HashMap<String, String>();
  map04.put("A", "STRING");
  map04.put("B", "x3x;");
  map04.put("C", "void");
  map04.put("D", "de$f");

  Map<String, String> map05 = new HashMap<String, String>();
  map05.put("A", "31");
  map05.put("B", "0");
  map05.put("C", "1");
  map05.put("D", "2");
  ArrayList<ChoiceQuestion> choiceQuestionList = new ArrayList<ChoiceQuestion>();
  choiceQuestionList.add(new ChoiceQuestion("JAVA所定义的版本中不包括", map01, "D"));
  choiceQuestionList.add(new ChoiceQuestion("下列说法正确的是", map02, "A"));
  choiceQuestionList.add(new ChoiceQuestion("以下()不是合法的标识符",map04, "C"));
  choiceQuestionList.add(new ChoiceQuestion("表达式(11+3*8)/4%3的值是", map05, "D"));
  choiceQuestionList.add(new ChoiceQuestion("变量命名规范说法正确的是", map03, "B"));
  //题目顺序打乱
  Collections.shuffle(choiceQuestionList);
  // 选项-答案乱序
  for (ChoiceQuestion question : choiceQuestionList) {
   Topic random = random(question.getOption(), question.getKey());
   question.setOption(random.getOption());
   question.setKey(random.getKey());
  }
  //输出打印
  System.out.println(toString(choiceQuestionList, candidate, number));
 }
 
 /**
  * 打乱选项和答案
  * @param option
  * @param key
  * @return
  */
 public static Topic random(Map<String, String> option, String key) {
  Set<String> keySet = option.keySet();
  ArrayList<String> keyList = new ArrayList<String>(keySet);
  Collections.shuffle(keyList);
  HashMap<String, String> optionNew = new HashMap<String, String>();
  int idx = 0;
  String keyNew = "";
  for (String next : keySet) {
   String randomKey = keyList.get(idx++);
   if (key.equals(next)) {
    keyNew = randomKey;
   }
   optionNew.put(randomKey, option.get(next));
  }
  return new Topic(optionNew, keyNew);
 }

 public static String toString(ArrayList<ChoiceQuestion> choiceQuestionList,String candidate, String number) {

  StringBuilder detail = new StringBuilder("考生:" + candidate + "\r\n" +
    "考号:" + number + "\r\n" +
    "--------------------------------------------\r\n" +
    "一、选择题" + "\r\n\n");

  for (int idx = 0; idx < choiceQuestionList.size(); idx++) {
   detail.append("第").append(idx + 1).append("题:").append(choiceQuestionList.get(idx).getName()).append("\r\n");
   Map<String, String> option = choiceQuestionList.get(idx).getOption();
   for (String key : option.keySet()) {
    detail.append(key).append(":").append(option.get(key)).append("\r\n");;
   }
   detail.append("答案:").append(choiceQuestionList.get(idx).getKey()).append("\r\n\n");
  }

  return detail.toString();
 }



 public static void main(String[] args) {
  output("花花", "1000001921032");
  output("豆豆", "1000001921051");
  output("大宝", "1000001921987");
 }
}

输出结果

考生:花花
考号:1000001921032
--------------------------------------------
一、选择题

第1题:JAVA所定义的版本中不包括
A:JAVA2 HE
B:JAVA2 ME
C:JAVA2 SE
D:JAVA2 Card
E:JAVA2 EE
答案: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:JAVA2 Card
B:JAVA2 EE
C:JAVA2 SE
D:JAVA2 HE
E:JAVA2 ME
答案: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:JAVA2 ME
B:JAVA2 Card
C:JAVA2 SE
D:JAVA2 HE
E:JAVA2 EE
答案: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题目选项乱序内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • java如何实时动态获取properties文件的内容

    java如何实时动态获取properties文件的内容

    这篇文章主要介绍了java如何实时动态获取properties文件的内容,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • Spring Boot打开URL出现signin问题的解决

    Spring Boot打开URL出现signin问题的解决

    这篇文章主要介绍了Spring Boot打开URL出现signin问题的解决,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-12-12
  • springboot2.2 集成 activity6实现请假流程(示例详解)

    springboot2.2 集成 activity6实现请假流程(示例详解)

    这篇文章主要介绍了springboot2.2 集成 activity6实现请假完整流程示例详解,本文通过示例代码图文相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-07-07
  • Java实时获取基金收益项目源码分享

    Java实时获取基金收益项目源码分享

    这篇文章主要介绍了Java实时获取基金收益项目源码分享,主要包括JAVA爬取天天基金网数据使用实例、应用技巧、基本知识点总结和需要注意事项,需要的朋友可以参考下
    2021-03-03
  • Mybatis把返回结果封装成map类型的实现

    Mybatis把返回结果封装成map类型的实现

    本文主要介绍了Mybatis把返回结果封装成map类型的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-03-03
  • java之函数式接口解读

    java之函数式接口解读

    这篇文章主要介绍了java之函数式接口,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-03-03
  • java使用compareTo实现一个类的对象之间比较大小操作

    java使用compareTo实现一个类的对象之间比较大小操作

    这篇文章主要介绍了java使用compareTo实现一个类的对象之间比较大小操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-09-09
  • Spring security实现对账户进行加密

    Spring security实现对账户进行加密

    这篇文章主要介绍了Spring security实现对账户进行加密,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03
  • java关键字super的骚操作详解

    java关键字super的骚操作详解

    关键字super在Java中用于引用当前类的父类(即超类)的构造方法、访问父类的成员变量和方法,它提供了一种方便的方式来处理继承关系中的父类操作,下面我们就来看看它有哪些骚操作吧
    2023-09-09
  • SpringBoot中注解实现定时任务的两种方式

    SpringBoot中注解实现定时任务的两种方式

    这篇文章主要介绍了SpringBoot中注解实现定时任务的两种方式,SpringBoot 定时任务是一种在SpringBoot应用中自动执行任务的机制,通过使用Spring框架提供的@Scheduled注解,我们可以轻松地创建定时任务,需要的朋友可以参考下
    2023-10-10

最新评论