java實(shí)現(xiàn)題目以及選項(xiàng)亂序的方法實(shí)例
前言
在現(xiàn)實(shí)生活中,考試是我們的必經(jīng)之路,但是線上考試總有一部分人抄寫答案,為了防止抄寫,將題目和答案打亂,防止抄襲。本文實(shí)現(xiàn)通過map輪詢時將選項(xiàng)亂序,最終答案也變化。
選擇題類 ChoiceQuestion
import java.util.Map;
/**
* 單選題
*/
public class ChoiceQuestion {
private String name; // 題目
private Map<String, String> option; // 選項(xiàng);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;
}
}
處理后選項(xiàng)答案臨時對象 Topic
public class Topic {
private Map<String, String> option; // 選項(xiàng);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", "變量由字母、下劃線、數(shù)字、$符號隨意組成;");
map03.put("B", "變量不能以數(shù)字作為開頭;");
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("以下()不是合法的標(biāo)識符",map04, "C"));
choiceQuestionList.add(new ChoiceQuestion("表達(dá)式(11+3*8)/4%3的值是", map05, "D"));
choiceQuestionList.add(new ChoiceQuestion("變量命名規(guī)范說法正確的是", map03, "B"));
//題目順序打亂
Collections.shuffle(choiceQuestionList);
// 選項(xiàng)-答案亂序
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));
}
/**
* 打亂選項(xiàng)和答案
* @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");
}
}
輸出結(jié)果
考生:花花
考號: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題:以下()不是合法的標(biāo)識符
A:x3x;
B:void
C:de$f
D:STRING
答案:B第4題:表達(dá)式(11+3*8)/4%3的值是
A:0
B:31
C:1
D:2
答案:D第5題:變量命名規(guī)范說法正確的是
A:變量不能以數(shù)字作為開頭;
B:A和a在java中是同一個變量;
C:變量由字母、下劃線、數(shù)字、$符號隨意組成;
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題:以下()不是合法的標(biāo)識符
A:void
B:x3x;
C:de$f
D:STRING
答案:A第4題:表達(dá)式(11+3*8)/4%3的值是
A:31
B:2
C:0
D:1
答案:B第5題:變量命名規(guī)范說法正確的是
A:A和a在java中是同一個變量;
B:變量由字母、下劃線、數(shù)字、$符號隨意組成;
C:變量不能以數(shù)字作為開頭;
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題:以下()不是合法的標(biāo)識符
A:STRING
B:de$f
C:void
D:x3x;
答案:C第4題:表達(dá)式(11+3*8)/4%3的值是
A:2
B:0
C:1
D:31
答案:A第5題:變量命名規(guī)范說法正確的是
A:不同類型的變量,可以起相同的名字;
B:變量不能以數(shù)字作為開頭;
C:變量由字母、下劃線、數(shù)字、$符號隨意組成;
D:A和a在java中是同一個變量;
答案:B
本文參考 bugstack⾍洞棧 作者設(shè)計(jì)模式一書原型模式 提取選項(xiàng)答案亂序邏輯修改
總結(jié)
到此這篇關(guān)于java實(shí)現(xiàn)題目以及選項(xiàng)亂序的文章就介紹到這了,更多相關(guān)java題目選項(xiàng)亂序內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring中XML schema擴(kuò)展機(jī)制的深入講解
這篇文章主要給大家介紹了關(guān)于Spring中XML schema擴(kuò)展機(jī)制的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09
詳解Spring Data Jpa當(dāng)屬性為Null也更新的完美解決方案
這篇文章主要介紹了詳解Spring Data Jpa當(dāng)屬性為Null也更新的完美解決方案,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-02-02
SpringBoot動態(tài)Feign服務(wù)調(diào)用詳解
Feign是Netflix公司開發(fā)的一個聲明式的REST調(diào)用客戶端; Ribbon負(fù)載均衡、 Hystrⅸ服務(wù)熔斷是我們Spring Cloud中進(jìn)行微服務(wù)開發(fā)非?;A(chǔ)的組件,在使用的過程中我們也發(fā)現(xiàn)它們一般都是同時出現(xiàn)的,而且配置也都非常相似2022-12-12
Spring Cloud重試機(jī)制與各組件的重試總結(jié)
這篇文章主要給大家介紹了關(guān)于Spring Cloud中重試機(jī)制與各組件的重試的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11
Java EasyExcel導(dǎo)出報(bào)內(nèi)存溢出的解決辦法
使用EasyExcel進(jìn)行大數(shù)據(jù)量導(dǎo)出時容易導(dǎo)致內(nèi)存溢出,特別是在導(dǎo)出百萬級別的數(shù)據(jù)時,你有遇到過這種情況嗎,以下是小編整理的解決該問題的一些常見方法,需要的朋友可以參考下2024-10-10
Java編程實(shí)現(xiàn)遍歷兩個MAC地址之間所有MAC的方法
這篇文章主要介紹了Java編程實(shí)現(xiàn)遍歷兩個MAC地址之間所有MAC的方法,涉及Java針對MAC的遍歷獲取與字符串轉(zhuǎn)換相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
SpringCloud分布式項(xiàng)目下feign的使用示例詳解
這篇文章主要介紹了SpringCloud分布式項(xiàng)目下feign的使用,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07
簡單談?wù)凷pringMVC轉(zhuǎn)發(fā)和重定向的區(qū)別
下面小編就為大家?guī)硪黄唵握務(wù)凷pringMVC轉(zhuǎn)發(fā)和重定向的區(qū)別。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06

