java项目一 标准化试题训练系统(二)

标准化试题训练系统

RamdomInitTestPaper类实现GiveTestPaper接口,其实例负责给出试卷(随机从题库中抽取若干道试题形成一张试卷)

package data;
import java.io.*;
import jxl.*;
import java.util.*;
import javax.swing.JOptionPane;
public class RandomInitTestPaper implements GiveTestPaper {
    //将试题放入试卷(出卷)
    TestPaper testPaper;        //试卷
    File fileExcel;
    Problem[] problem;
    //组成试卷的一套题(problem的单元存放一道试题,即一个Problem对象)
    InputStream in = null;
    Workbook wb = null;
    //封装Excel,Workbook是jxl包中的类
    Sheet sheet = null; //封装 Excel中的sheet, Sheet是jxl包中的类
    LinkedList<Integer> list;
    //随机抽取试题时用
    static String[] correctAns;

    public RandomInitTestPaper() {
        testPaper = new TestPaper();
        list = new LinkedList<Integer>();
    }

    public static String[] getCorrectAns() {
        return correctAns;
    }

    @Override
    public TestPaper getTestPaper(String excelFileName, int amount) {
        boolean b = setExcel(excelFileName); // 设置用户存放试题的电子表格
        if (b) {
            try {
                randomGiveProblem(amount); //随机给出amount道试题,见类后面的random GiveProblem 方法
            } catch (ArrayIndexOutOfBoundsException e) {
                System.out.println("试题必须有类型,请检查题库");
                System.exit(0);
            }
            testPaper.setProblem(problem); //试卷上设置的一套试题是problem
            return testPaper;
            //返回试卷
        } else {
            JOptionPane.showMessageDialog(null, "没有预备题库", "消息对话框", JOptionPane.WARNING_MESSAGE);
            return null;
        }
    }

    private boolean setExcel(String excelFileName) {
        boolean b = true;
        try {
            fileExcel = new File(excelFileName);
            in = new FileInputStream(fileExcel);
            testPaper.setProblemSource(fileExcel.getAbsolutePath()); //试卷设置题库来源
        } catch (IOException exp) {
            JOptionPane.showMessageDialog(null, "没有预备题库Excel", "消息对话框", JOptionPane.WARNING_MESSAGE);
            b = false;
        }
        try {
            wb = Workbook.getWorkbook(in);
            in.close();
        } catch (Exception exp) {
            b = false;
        }
        return b;
    }
    private void randomGiveProblem (int amount) {
        //随机给出amount道试题放入problem数组中
        list.clear() ;
        if (wb==null) {
            JOptionPane.showMessageDialog(null, "没有预备题库Excel", "消息对话框", JOptionPane.WARNING_MESSAGE);
            return;
        }
        sheet = wb.getSheet (0) ;
        //得到Excel中的第一个sheet (索引从0开始)
        int rowsAmount = sheet.getRows(); //得到sheet的总行数
        //注意原始Excel表中sheet中的第0行不是试题,是用户输入的说明
        int realAmount = Math.min(amount, rowsAmount-1) ; //实际抽取的试题数量
        problem = new Problem[realAmount]; //用于存放试题的数组problem
        correctAns = new String[realAmount];
        for (int i = 0; i < rowsAmount - 1; i++) { //将1~rowsAmount-1放入链表
            list.add(i + 1);
        }
        Random random=new Random();
        for(int i = 0; i < problem.length; i++) {
            int m = random.nextInt(list.size()); //[O, list.size())中的一个随机数
            int index = list.remove(m);//删除list的第m个节点,同时得到节点数字
            Cell[] cell = sheet.getRow(index); //返回sheet中的第index行
            //注意原始Excel表中sheet中的第0行不是试题,是用户输入的说明.
            //cell的第0列是试题内容,索引从0开始
            problem[i] = new Problem();
            int number = i + 1;
            problem[i].setContent ("第" + number + "题." + cell[0].getContents());//试题的内容
            problem[i].setCorrectAnswer(cell[1].getContents().trim());//试题的答案
            problem[i].setGiveChoiceA(cell[2].getContents().trim());//试题的A选择
            problem[i].setGiveChoiceB(cell[3].getContents().trim());//试题的B选择
            problem[i].setGiveChoiceC(cell[4].getContents().trim());//试题的C选择
            problem[i].setGiveChoiceD(cell[5].getContents ().trim());//试题的D答案
            String typeStr = cell[6].getContents().trim();//试题的类型(判断或选择)
            correctAns[i] = problem[i].getCorrectAnswer();
            //因为试题有图像,所以typeStr有4种,即p、p#、 x、x#
            if (typeStr.equalsIgnoreCase("p")) {
                problem[i].setIsJudge(true);
                problem[i].setIsChoice(false);
                problem[i].setImageName("havenot.jpg");
            }
            if (typeStr.equalsIgnoreCase ("x")) {
                problem[i].setIsJudge(false);
                problem[i].setIsChoice(true);
                problem[i].setImageName("havenot.jpg");
            }
            if (typeStr.startsWith("p#") || typeStr.startsWith("P#")) {
                problem[i].setIsJudge(true);
                problem[i].setIsChoice(false);
                String imageName = typeStr.substring(typeStr.indexOf("#") + 1);
                problem[i].setImageName(imageName);
            }
            if (typeStr.startsWith ("x#") || typeStr.startsWith ("X#")) {
                problem[i].setIsJudge(false);
                problem[i].setIsChoice(true);
                String imageName = typeStr.substring(typeStr.indexOf ("#") + 1);
                problem[i].setImageName (imageName);
            }
        }
    }
}

  • 2
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小倪长头发啦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值