java 从文件夹随机抽取三题目 每道题选项的个数,正确答案,难度系数以及分数由每道题第一行决定 保存所有的题目,随机的从题库中输入三道题,用户进行答题,最终计算出该用户的得分

img

img
每道题选项的个数,正确答案,难度系数以及分数由每道题第一行决定
保存所有的题目,随机的从题库中输入三道题,用户进行答题,最终计算出该用户的得分
每道题选项的个数,正确答案,难度系数以及分数由每道题第一行决定
保存所有的题目,随机的从题库中输入三道题,用户进行答题,最终计算出该用户的得分

文本文档可以用java读取,但是内容过于杂乱很难解析,没有办法做成答题系统,除非用Excel来装题目

先写一个对象,{"选项个数","本题答案","分值","难度级别","题目的问题","具体选项(list来装,因为你前面的答案为数字可以当做下标来取值)"}
然后读取文件以@来做每一题的第一行(主要信息)然后识别出有几个选项(后面需要读取几行,选项+1行)。再new出一个这个对象,把全部题都放进一个list中。随机三个下标取出三个题。


public class TestMain {

    public static void main(String[] args) throws IOException {

        // 将文本所有的题录入到系统中
        List<Topic> topics = new ArrayList<>();
        File file = new File("D:\\Backup\\桌面\\core.txt");
        BufferedReader br = new BufferedReader(new FileReader(file));
        String s;
        while((s=br.readLine()) != null) {
            if (s.startsWith("@option=")) {
                Topic topic = new Topic();
                String[] info = s.substring(1).split(",");
                Integer option = new Integer(info[0].split("=")[1]);
                topic.setOption(option);
                topic.setAnswers(Arrays.asList(info[1].split("=")[1].split("/")).stream().mapToInt(Integer::parseInt).toArray());
                topic.setScore(new Integer(info[2].split("=")[1]));
                topic.setLevel(new Integer(info[3].split("=")[1]));
                topic.setResults(new ArrayList<>());
                for (Integer i = 0; i <= option; i++) {
                    s = br.readLine();
                    if (i == 0) {
                        topic.setQuestion(s);
                    }else {
                        topic.getResults().add(s);
                    }
                }
                topics.add(topic);
            }
        }
        br.close();
    }
}

class Topic{

    private Integer option;
    private int[] answers;
    private Integer score;
    private Integer level;
    private String question;

    private List<String> results;

    @Override
    public String toString() {
        return "Topic{" +
                "option=" + option +
                ", answers=" + Arrays.toString(answers) +
                ", score=" + score +
                ", level=" + level +
                ", question='" + question + '\'' +
                ", results=" + results +
                '}';
    }

    public Integer getOption() {
        return option;
    }

    public void setOption(Integer option) {
        this.option = option;
    }

    public int[] getAnswers() {
        return answers;
    }

    public void setAnswers(int[] answers) {
        this.answers = answers;
    }

    public Integer getScore() {
        return score;
    }

    public void setScore(Integer score) {
        this.score = score;
    }

    public Integer getLevel() {
        return level;
    }

    public void setLevel(Integer level) {
        this.level = level;
    }

    public String getQuestion() {
        return question;
    }

    public void setQuestion(String question) {
        this.question = question;
    }

    public List<String> getResults() {
        return results;
    }

    public void setResults(List<String> results) {
        this.results = results;
    }
}