(一)初始化操作实现
如上图所示当管理员需要更改适用工程、试题数量、考试时间时直接在文本中更改就好我们只需要每次在用户打开程序时初始化这些参数就可以
(二)具体实现
初始化试题模型,这里需要实现随机抽题,方法是用链表来模拟随机抽题
1.InitQuestionModel
public class InitQuestionModel { QuestionModel mQuestionModel; Questions[] questions=null; String[][] record;//数据库读取的记录 Connection con; PreparedStatement ppStatement; PreQuery query; int mNumber=0;//试题数量 LinkedList<Integer> list;//随机抽题所用链表 String SQL="select * from Question where adaptEngineering = "; public QuestionModel getQuestionModel(String adaptEngineering,int QuestionNumbers) { //获取对应工程下所有试题 SQL=SQL+"'"+adaptEngineering+"'"; query.setDatabaseName("Examination"); query.setSQL(SQL); query.startQuery(); record=query.getRecord(); //获取全部试题数量 int recordLength=record.length; //判断试题数量是否足够考试要求数量 if(QuestionNumbers>recordLength) { JOptionPane.showMessageDialog(null,"题库试题数量少于要求数量请重新设定考试题数量!","消息对话框", JOptionPane.WARNING_MESSAGE); return null; }else { //使节点数量和总题量相同 for(int i=0;i<recordLength;i++) { list.add(i); } //随机数 Random random=new Random(); //问题数量与考试要求数量相同 questions=new Questions[QuestionNumbers]; for(int i=0;i<QuestionNumbers;i++) { //模拟随机抽题产生随机数,要注意随机数的上限要跟随链表长度变化 int mRandom=random.nextInt(list.size()); //抽到则从表中剔除 int index=list.remove(mRandom); //为试题添加相应内容 questions[i]=new Questions(); questions[i].setNumber(i); if(record[index][3].equals("1")) { questions[i].setType("单项选择"); }else if(record[index][3].equals("2")) { questions[i].setType("多项选择"); } else if(record[index][3].equals("3")) { questions[i].setType("判断题"); } questions[i].setContext(record[index][4]); questions[i].setImageName(record[index][5]); questions[i].setChoiceAContext(record[index][6]); questions[i].setChoiceBContext(record[index][7]); questions[i].setChoiceCContext(record[index][8]); questions[i].setChoiceDContext(record[index][9]); questions[i].setCurrectAnswer(record[index][10]); //判断试题类型 if(record[index][3].equals("1")) { questions[i].setIsChoice(true); questions[i].setIsMultipleChoice(false); questions[i].setIsJudgement(false); } else if(record[index][3].equals("2")) { questions[i].setIsChoice(false); questions[i].setIsMultipleChoice(true); questions[i].setIsJudgement(false); }else if(record[index][3].equals("3")) { questions[i].setIsChoice(false); questions[i].setIsMultipleChoice(false); questions[i].setIsJudgement(true); } } mQuestionModel=new QuestionModel(); mQuestionModel.setQuestions(questions); return this.mQuestionModel; } } InitQuestionModel() { query=new PreQuery(); list=new LinkedList<Integer>(); } }
初始化考试
2.InitExamHandle
public class InitExamHandle implements ActionListener{ InitExamView mView; String examType; String filePathQuestionAmount="./试题管理目录/试题数量.txt"; String filePathExamTimes="./试题管理目录/考试时间(单位为分钟).txt"; int[] mExamTimes; int[] mQuestionAmount; Scanner scanner; public void setInitExamView(InitExamView view) { this.mView=view; } //得到每个工程所需考试题数量 public int getQuestionAmount(int index) { mQuestionAmount=new int[8]; try { //考试题数量存在文本文件中,每次进入程序每次重新获取 scanner=new Scanner(new File(filePathQuestionAmount)); //试题数量以换行符隔开 scanner.useDelimiter("[\n]+"); for(int i=0;i<mQuestionAmount.length;i++) { mQuestionAmount[i]=Integer.parseInt(scanner.next()); } } catch (FileNotFoundException e) { e.printStackTrace(); } return mQuestionAmount[index]; } //得到考试时间 public int getExamTime(int index) { mExamTimes=new int[8]; try { scanner=new Scanner(new File(filePathExamTimes)); scanner.useDelimiter("[\n]+"); for(int i=0;i<mExamTimes.length;i++) { mExamTimes[i]=Integer.parseInt(scanner.next()); } } catch (FileNotFoundException e) { e.printStackTrace(); } return mExamTimes[index]; } @Override public void actionPerformed(ActionEvent e) { int id=mView.mApplicable.getSelectedIndex(); examType=mView.mApplicable.getSelectedItem().toString(); int mQuestionNumber=getQuestionAmount(id); int mExamTime=getExamTime(id); InitQuestionModel mInitQuestionModel=new InitQuestionModel(); QuestionModel mQuestionModel= mInitQuestionModel.getQuestionModel(examType,mQuestionNumber); if(mQuestionModel!=null) { //如果成功获取试题模型则显示考试界面 ExamWorkView examWorkView =new ExamWorkView("考试窗口",mQuestionModel,mQuestionNumber,mExamTime); }else { JOptionPane.showMessageDialog(null,"考试窗口初始化失败!请检查试用工程是否在题库中存在或试题数量是否超过上限!", "提示", JOptionPane.WARNING_MESSAGE); } } }
点击下载源码ExamProject
打赏
微信扫一扫~听说打赏我的人,都进了福布斯排行榜!
Your first apaerpance, he said to me, is the gauge by which you will be measured; try to manage that you may go beyond yourself in after times, but beware of ever doing less.
I do not understand what you mean, could you explain it for me? I want to know what are my drawbacks and I will correct them, thank you very much.