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:https://github.com/wyd333/RandomQuestionPickericon-default.png?t=N7T8https://github.com/wyd333/RandomQuestionPicker 

  • 13
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值