Java课程设计-随机组卷程序(仅仅包含选择题)2.0版本

代码逻辑

  1. 读取文件
  2. 抽取选择题
  3. 随机调换选择题顺序
  4. 获取选择题前resultCount个为结果集合
  5. 将结果集合转换为字符串
  6. 字符串文件流输出。

代码

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: ycf
 * @description:
 * @Date: 2021/2/7 11:40
 */
public class CreateMyTest {


    public static void main(String[] args) {
        try {
            //1. 读取文件
            List<String> content = readFile("C:\\Users\\Faker\\Desktop\\测试习题.txt");

            // 2. 抽取选择题
            List<Question> questionList = readContent(content);

            // 3. 随机调换选择题顺序
            disruptTheOrder(questionList);

            // 4. 获取选择题前**resultCount**个为结果集合
            List<Question> resultList = new ArrayList<>();
            int resultCount = questionList.size() > 30 ? 30 : questionList.size();// 生成题目的数量
            for( ; resultCount > 0; resultCount--){
                resultList.add(questionList.get(resultCount-1));
            }

            StringBuffer sb = new StringBuffer();
            StringBuffer answer = new StringBuffer();
            // 5. 将结果集合转换为字符串
            resultListToStr(resultList, sb, answer);

            // 6. 字符串文件流输出。
            createFile(sb, answer, "C:\\Users\\Faker\\Desktop\\result.doc");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("兄弟,你代码报错啦!!!");
        }


    }

    /**
     * 1. 读取文件
     * 文件读取结束符号为 : ******  别让你的代码循环死喽
     * @param filePath 待读取文件的路径
     * @return
     */
    private static List<String> readFile(String filePath) {
        if(StringUtils.isEmpty(filePath)) {
            System.out.println("兄弟,记得要填写文件的绝对路径!!!");
            return null;
        }

        // 读取文件 可参考内容格式
        List<String> content = null;
        try {
            FileReader reader = new FileReader(filePath);
            BufferedReader br = new BufferedReader(reader);//创建一个BufferedReader缓冲对象
            String line = null;
            content = new ArrayList<>();
            // (line = br.readLine()) 运算结果为 br.readLine()的内容,故(line = br.readLine())等于 ****** 代表文件读取完毕
            while(!"******".equals(line = br.readLine())){
                // 过滤空行 StringUtils.isNotBlank(line.trim())
                if(StringUtils.isNotBlank(line.trim())){
                    content.add(line.trim());
                }
            }
            br.close();
        } catch (FileNotFoundException e) {
            System.out.println("兄弟,找不到你家文件在哪!!!");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("兄弟,读取文件失败 或者 关闭资源失败!!!");
            e.printStackTrace();
        }
        return content;
    }

    /**
     * 2. 抽取选择题
     * 注意:文件中题目必须符合一定的格式 eg:第一行是题目以及答案以"$"开头,已"(X)"结束,第二行是四个选项以"A."开头
     * 遇到 "#"代表本行为注释,可以忽略
     * @param content
     * @return
     */
    private static List<Question> readContent(List<String> content) {
        String title = null,a, b,c,d,result = null;
        List<Question> questionList = new ArrayList<>();
        try {
            for (String str : content) {
                if (str.startsWith("$")) {
                    // 题目:该行数据 第一个"."到最后一个"("之间的字符
                    title = str.substring(str.indexOf(".")+1, str.lastIndexOf("("));
                    result = str.substring(str.lastIndexOf("(") + 1, str.lastIndexOf(")"));
                } else if (str.startsWith("A.")) {
                    // 获取四个选项的答案
                    a = str.substring(str.indexOf("A.") + 2, str.indexOf("B.")).trim();
                    b = str.substring(str.indexOf("B.") + 2, str.indexOf("C.")).trim();
                    c = str.substring(str.indexOf("C.") + 2, str.indexOf("D.")).trim();
                    d = str.substring(str.indexOf("D.") + 2).trim();
                    questionList.add(new Question(title, a, b, c, d, result));
                }
            }
        } catch (Exception e){
            System.out.println("兄弟,你的文件格式不对啊!!!");
            e.printStackTrace();
        }
        return questionList;
    }

    /**
     * 3. 随机调换选择题顺序
     * 随机生成两个不同的数据,数字范围:0 ~ questionList.size()-1
     * @param questionList
     */
    private static void disruptTheOrder(List<Question> questionList) {
        if(CollectionUtils.isEmpty(questionList))
            return;

        int min = 0, max = questionList.size() - 1;
        int first,second;// 交换位置的俩对象下标

        int cycleTime = 30; // 设置循环次数:题目交换位置的次数
        Question temp = null; // 交换题目位置时,临时保存的对象
        for( ;cycleTime > 0; cycleTime--){
            // first 和 second 相同时 可以忽略
            first = (int) (Math.random() * (max - min + 1) + min);
            second = (int) (Math.random() * (max - min + 1) + min);

            temp = questionList.get(first);
            questionList.set(first, questionList.get(second));
            questionList.set(second, temp);
        }
    }

    /**
     * 5. 将结果集合转换为字符串 (StringBuffer)
     * @param resultList
     * @param sb
     * @param answer
     */
    private static void resultListToStr(List<Question> resultList, StringBuffer sb, StringBuffer answer) {
        answer.append("答案:\n");
        sb.append("  \t\t\t\t\t\t\t 傻瓜测试题  \n");
        sb.append("  \t\t\t\t\t\t\t\t\t\t\t\t\t 出题人:木易唐唐 \n");
        int resultCount = resultList.size();
        for(int i = 1; i <= resultCount; i++){
            Question item = resultList.get(i-1);
            sb.append(i + "." + item.getTitle() + "\n");
            sb.append("A." + item.getA() + "\t\tB." + item.getB() + "\nC." + item.getB() + "\t\tD." + item.getD() + "\n\n");

            // 获取答案,每5题换行一次
            answer.append("第" + i + "题:" + item.getResult() + "\t\t");
            if(i % 5 == 0)
                answer.append("\n");
        }
    }

    /**
     * 6. 字符串文件流输出。
     * @param sb 题目
     * @param answer 答案
     * @param filePath 生成结果文件绝对路径
     */
    private static void createFile(StringBuffer sb, StringBuffer answer, String filePath) {
        try{
            // 将StringBuffer输出为文件
            FileWriter writer = new FileWriter(filePath);
            BufferedWriter bw = new BufferedWriter(writer);
            bw.write(sb.toString());
            bw.write(answer.toString());
            bw.flush();
            bw.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println("兄弟,先把你word文件关闭了再生成!!!");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("兄弟,生成word文件时报错!!!");
        }
    }
}

class Question{
    // 题目
    private String title;
    // 答案a
    private String a;
    // 答案b
    private String b;
    // 答案c
    private String c;
    // 答案d
    private String d;
    // 正确答案
    private String result;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getA() {
        return a;
    }

    public void setA(String a) {
        this.a = a;
    }

    public String getB() {
        return b;
    }

    public void setB(String b) {
        this.b = b;
    }

    public String getC() {
        return c;
    }

    public void setC(String c) {
        this.c = c;
    }

    public String getD() {
        return d;
    }

    public void setD(String d) {
        this.d = d;
    }

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

    public Question(String title, String a, String b, String c, String d, String result) {
        this.title = title;
        this.a = a;
        this.b = b;
        this.c = c;
        this.d = d;
        this.result = result;
    }

    @Override
    public String toString() {
        return "Question{" +
                "title='" + title + '\'' +
                ", a='" + a + '\'' +
                ", b='" + b + '\'' +
                ", c='" + c + '\'' +
                ", d='" + d + '\'' +
                ", result='" + result + '\'' +
                '}';
    }
}

题目模板

## 请严格执行本文件格式
$1.How old are you?(D)
A.13 B.18 C.20 D.26

$2.Do you have a lover?(A)
A.not B.yes C.yes,I do. D.No,I donot

$3.中国共产党第十九次全国代表大会,是在全面建成小康社会决胜阶段、中国特色社会主义进入_____的关键时期召开的一次十分重要的大会。(D)
A.新时期 B.新阶段 C.新征程 D.新时代

$4.十九大的主题是:不忘初心,____,高举中国特色社会主义伟大旗帜,决胜全面建成小康社会,夺取新时代中国特色社会主义伟大胜利,为实现中华民族伟大复兴的中国梦不懈奋斗。(B)
A.继续前进 B.牢记使命 C.方得始终 D.砥砺前行

$5.中国共产党人的初心和使命,就是为中国人民____ ,为中华民族____。这个初心和使命是激励中国共产党人不断前进的根本动力。(C)
A.谋幸福,谋未来 B.谋生活,谋复兴 C.谋幸福,谋复兴 D.谋生活,谋未来

$6.五年来,我们统筹推进“____”总体布局、协调推进“____”战略布局,“十二五”规划胜利完成,“十三五”规划顺利实施,党和国家事业全面开创新局面。(A)
A.五位一体 四个全面 B.四位一体 五个全面 C.五个全面 四位一体 D.四个全面 五位一体

$7.过去五年,经济保持中高速增长,在世界主要国家中名列前茅,国内生产总值从五十四万亿元增长到____万亿元,稳居世界第二,对世界经济增长贡献率超过百分之三十。(C)
A.六十 B.七十 C.八十 D.九十

$8.脱贫攻坚战取得决定性进展,____贫困人口稳定脱贫,贫困发生率从百分之十点二下降到百分之四以下。(A)
A.六千多万 B.七千多万  C.八千多万 D.九千多万

$9.实施共建“一带一路”倡议,发起创办亚洲基础设施投资银行,设立丝路基金,举办首届“一带一路”国际合作高峰论坛、亚太经合组织领导人非正式会议、二十国集团领导人____峰会、金砖国家领导人____会晤、亚信峰会。(B)
A.北京 南京 B.杭州 厦门  C.南京 北京 D.厦门 杭州

$10.坚持反腐败无禁区、全覆盖、零容忍,坚定不移“打虎”、“拍蝇”、“猎狐”,____的目标初步实现,____的笼子越扎越牢,____的堤坝正在构筑,反腐败斗争压倒性态势已经形成并巩固发展。(A)
A.不敢腐 不能腐 不想腐  B.不能腐 不敢腐 不想腐  C.不想腐 不敢腐 不能腐  D.不敢腐 不想腐 不能腐

******

结果演示

在这里插入图片描述

碎碎念

时隔三年,闲暇之余,今日做一个2.0版本,一是因为1.0版本的博客是本人浏览量最多的一篇,偶尔还会有一些朋友会下面留言,询问一点代码逻辑。其实在写1.0版本时,本人刚学java,所学所会一知半解,代码坑坑拌拌,为了应付那个时候的作业,代码能运行起来已经是“奇迹”,hpu的伙伴们要要对自己有更高要求哈。二是步入工作后,一直以来比较麻木,专业技能在一点点退步,想找借口可以有很多,比如公司没用到,平时不喜欢学习,王者荣耀太上瘾等等。20210207算是一个开始吧,往后去我会逼迫自己去接触新的技能,毕竟在不学习怎么赚钱找对象。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值