十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
这篇文章主要为大家详细介绍了Java实现在线测评考试系统的方法,文中示例代码介绍的非常详细,零基础也能参考此文章,感兴趣的小伙伴们可以参考一下。
创新互联主营湘潭网站建设的网络公司,主营网站建设方案,成都App制作,湘潭h5微信小程序开发搭建,湘潭网站营销推广欢迎湘潭等地区企业咨询
代码展示
package service;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import util.Config;
import util.Md5Utils;
import entity.EntityContext;
import entity.ExamInfo;
import entity.Question;
import entity.QuestionInfo;
import entity.User;
import exception.IdOrPasswordException;public class ExamServiceImpl implements ExamService {
private EntityContext entityContext;
private Listpaper = new ArrayList ();
private Config config;
private User loginUser;
public List
getPaper() { return paper; } public void setPaper(List
}paper) { this.paper = paper; } public ExamServiceImpl(EntityContext entityContext, Config config) { super(); this.entityContext = entityContext; this.config = config; } public ExamServiceImpl(EntityContext entityContext) { super(); this.entityContext = entityContext; } @Override public User login(int id, String password) throws IdOrPasswordException { loginUser = entityContext.findUserById(id); if (loginUser == null) { throw new IdOrPasswordException("鏃犳鐢ㄦ埛!"); } if (loginUser.getPassword().equals(Md5Utils.md5(password))) { return loginUser; } throw new IdOrPasswordException("瀵嗙爜閿欒!"); } @Override public ExamInfo start() { buildPaper(); ExamInfo examInfo = new ExamInfo(); examInfo.setUser(loginUser); examInfo.setTimeLimit(config.getInt("TimeLimit")); examInfo.setExamTitle(config.getString("PaperTitle")); examInfo.setQuestionNumber(config.getInt("QuestionNumber")); return examInfo; } private void buildPaper() { int i = 0; Random random = new Random(); for (int level = Question.LEVEL1; level <= Question.LEVEL10; level++) { List list = entityContext.getQuestions(level); Question q1 = list.remove(random.nextInt(list.size())); Question q2 = list.remove(random.nextInt(list.size())); paper.add(new QuestionInfo(++i, q1)); paper.add(new QuestionInfo(++i, q2)); } } @Override public QuestionInfo getQuestionInfo(int index) { return paper.get(index - 1); } @Override public void sendUserAnswers(int questionIndex, List answers) { QuestionInfo questionInfo = paper.get(questionIndex - 1); questionInfo.setUserAnswers(new ArrayList (answers)); } @Override public int getTotalSocre() { int score = 0; for (QuestionInfo questionInfo : paper) { if (questionInfo.getUserAnswers().equals( questionInfo.getQuestion().getAnswers())) { score += questionInfo.getQuestion().getScore(); } } return score; }
看完这篇文章,你们学会Java实现在线测评考试系统的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!