RandomQuestionPicker简单的随机抽题系统

简介: 一个简单的随机抽题系统,题库以文件的方式读入程序,功能是随机抽题并记录某题抽取次数。刚好有需要,给自己写了个,顺便开源。

一个简单的随机抽题系统,题库以文件的方式读入程序,功能是随机抽题并记录某题抽取次数。刚好有需要,给自己写了个,顺便开源。


没做UI界面。需要的同学自取即可。


使用时将questions.txt文件和src并列放到Project目录下,questions.txt文件就是题库文件。里面录入题干的格式是:


题目内容;0

注意是英文分号,以及末尾加上数字0代表共被抽取了0次。这个数字会在程序运行后更改。


未来可能会迭代带权重的随机抽题程序。


代码:


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
 
public class RandomQuestionPicker {
    private static final String FILE_PATH = "questions.txt";
    private static final String SEPARATOR = ";";
 
    public static void main(String[] args) {
        Map<String, Integer> questionCounts = readQuestionCountsFromFile();
 
        if (questionCounts.isEmpty()) {
            System.out.println("题库为空!");
            return;
        }
 
        String selectedQuestion = pickRandomQuestion(questionCounts);
        System.out.println("随机抽取的题目是:\n" + selectedQuestion);
 
        incrementQuestionCount(questionCounts, selectedQuestion);
        writeQuestionCountsToFile(questionCounts);
    }
 
    private static Map<String, Integer> readQuestionCountsFromFile() {
        Map<String, Integer> questionCounts = new HashMap<>();
        try (BufferedReader reader = new BufferedReader(new FileReader(FILE_PATH))) {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] parts = line.split(SEPARATOR);
                if (parts.length == 2) {
                    String question = parts[0];
                    int count = Integer.parseInt(parts[1]);
                    questionCounts.put(question, count);
                }
            }
        } catch (IOException e) {
            System.err.println("读取文件出错:" + e.getMessage());
        }
        return questionCounts;
    }
 
    private static String pickRandomQuestion(Map<String, Integer> questionCounts) {
        Random random = new Random();
        int randomNumber = random.nextInt(questionCounts.size()) + 1;
        int accumulatedWeight = 0;
        for (Map.Entry<String, Integer> entry : questionCounts.entrySet()) {
            accumulatedWeight += 1;
            if (randomNumber == accumulatedWeight) {
                return entry.getKey();
            }
        }
        return null;
    }
 
    private static void incrementQuestionCount(Map<String, Integer> questionCounts, String question) {
        int count = questionCounts.getOrDefault(question, 0);
        questionCounts.put(question, count + 1);
    }
 
    private static void writeQuestionCountsToFile(Map<String, Integer> questionCounts) {
        try (FileWriter writer = new FileWriter(FILE_PATH)) {
            for (Map.Entry<String, Integer> entry : questionCounts.entrySet()) {
                writer.write(entry.getKey() + SEPARATOR + entry.getValue() + "\n");
            }
        } catch (IOException e) {
            System.err.println("写入文件出错:" + e.getMessage());
        }
    }
}

运行:




Github

目录
打赏
0
1
1
0
31
分享
相关文章
【C++ 随机数分布类型 】深入探索C++随机数分布:原理、应用与实践(二)
【C++ 随机数分布类型 】深入探索C++随机数分布:原理、应用与实践
243 0
【C++ 随机数分布类型 】深入探索C++随机数分布:原理、应用与实践(二)
|
9月前
|
随机读写
随机读写
83 0
关于随机点
关于随机点
67 0
|
9月前
用R语言模拟随机服务排队系统
用R语言模拟随机服务排队系统
|
9月前
用R语言模拟M / M / 1随机服务排队系统
用R语言模拟M / M / 1随机服务排队系统
【C++ 随机数分布类型 】深入探索C++随机数分布:原理、应用与实践(一)
【C++ 随机数分布类型 】深入探索C++随机数分布:原理、应用与实践
272 0
加权随机设计与实现
加权随机,是指当我们从某种容器中随机选择一个元素,每个元素被选中的机会并不相等,而是由相对“权重”(或概率)被选中的,也就是说我们想要有“偏心”的得到某种随机结果。
78933 1
加权随机设计与实现
机器器学习算法系列列(1):随机森林
随机森林原理 顾名思义,是用随机的方式建立一个森林,森林里面有很多的决策树组成,随机森林的每一棵决 策树之间是没有关联的。在得到森林之后,当有一个新的输入样本进入的时候,就让森林中的每 一棵决策树分别进行一下判断,看看这个样本应该属于哪一类(对于分类算法),然后看看哪一 类被选择最多,就预测这个样本为那一类。
1221 0