学生 进入 考试系统 考试 需要输入 名字 和 学号
老师 修改 学生的 试卷
考试系统中 有十道题 每次学生考试 会从题库中 随机抽取五道题
考试前系统会检索学生信息 信息正确 才能开始考试
public class Question {//面向对象的编程思想 将一道题目看成对象//属性 题目 答案private String tittle;private String answer;public Question(String tittle,String answer){this.tittle = tittle;this.answer = answer;}//重写equals方法public boolean equals(Object obj){if(this==obj){return true;}if(obj instanceof Question){Question anotherQuestion = (Question) obj;//按照?截取题目String[] question1 = this.tittle.split("?");String[] question2 = anotherQuestion.tittle.split("?");if(question1[0].equals(question2[0])){return true;}}return false;}//重写hashCode方法public int hashCode(){String[] question = this.tittle.split("?");return question[0].hashCode();}public String getTittle(){return this.tittle;}public String getAnswer(){return this.answer;}}
public class Student {private String name;private String num;public Student(String name, String num) {this.name = name;this.num = num;}public String getName() {return this.name;}public String getNum() {return this.num;}//学生答题public String[] answer(ArrayList<Question> testPaper){String[] stuAnswer = new String[testPaper.size()];Scanner input = new Scanner(System.in);for(int i = 0;i<testPaper.size();i++){System.out.println((i+1)+"."+testPaper.get(i).getTittle());System.out.println("请输入您的答案:");stuAnswer[i] = input.nextLine();}return stuAnswer;}}
public class TestSystem {//考试系统//属性---题库 题库中存有很多Question对象 每一个对象都是一道题目//可以考虑Set集合 无序无重复 这样重复的题目会拒绝录入//所以需要重写equals方法和hashCode方法private HashMap<String,String> stuBOx = new HashMap<>();//记录学生信息private HashSet<Question> testBox = new HashSet<Question>();{stuBOx.put("小兰","123");stuBOx.put("小明","456");stuBOx.put("小程","789");stuBOx.put("小红","000");}{testBox.add(new Question("以下哪个不是java的基本类型?\n\tA.short\n\tB.boolean\n\tC.String\n\tD.char","C"));//CtestBox.add(new Question("以下哪个是java的基本类型?\n\tA.Short\n\tB.Boolean\n\tC.String\n\tD.char","D"));//DtestBox.add(new Question("以下哪个数最大?\n\tA.5\n\tB.6\n\tC.7\n\tD.8","D"));//DtestBox.add(new Question("以下哪个数最小?\n\tA.5\n\tB.6\n\tC.7\n\tD.8","A"));//AtestBox.add(new Question("以下哪个是java的引用类型?\n\tA.String\n\tB.boolean\n\tC.int\n\tD.char","A"));//AtestBox.add(new Question("以下哪个不是java的引用类型?\n\tA.String\n\tB.Integer\n\tC.Boolean\n\tD.char","D"));//DtestBox.add(new Question("以下哪个不是java的包装类?\n\tA.Short\n\tB.Boolean\n\tC.String\n\tD.Character","C"));//CtestBox.add(new Question("以下哪个是java的包装类?\n\tA.short\n\tB.Boolean\n\tC.String\n\tD.char","B"));//BtestBox.add(new Question("以下哪个是java的权限修饰符?\n\tA.protected\n\tB.static\n\tC.final\n\tD.abstract","A"));//AtestBox.add(new Question("以下哪个是java的特征修饰符?\n\tA.public\n\tB.final\n\tC.protected\n\tD.private","B"));//B}//若用户名或密码错误 重新输入private Student loginAgain(){System.out.println("用户名或密码错误,请重新输入:");Scanner input = new Scanner(System.in);String stuName = input.nextLine();System.out.println("请输入您的学号");String stuNum = input.nextLine();Student stu = new Student(stuName,stuNum);return stu;}//判断学生姓名与学号 和 系统信息是否一致public void login(Student stu){String realPassword = stuBOx.get(stu.getName());//判断这个人是不是真的存在 如果不存在为nullif(realPassword!=null && realPassword.equals(stu.getNum())) {//如果人真的存在System.out.println("登录成功,考试即将开始,系统正在随机生成试卷,请梢等!");}else {login(loginAgain());}}//随机生成五道题目 返回值--->试卷//试卷应该也和题库一样 但是题目只有五道 试卷应该也是一个集合Set<Question>//给学生答题需要遍历一题一题来答 所以试卷可以是ArrayList<Question>public ArrayList<Question> getTestPaper(){//因为试卷的五道题应该是不重复的 所以不重复的考虑Set集合HashSet<Question> paper = new HashSet<>();//存储生成的试卷//先用Set集合存 然后最后再变成ArrayListArrayList<Question> testPaper = new ArrayList<>(this.testBox);//存储试卷while (paper.size() != 5) {//当时卷存满五道题后 循环结束//产生一个随机的序号去找寻题目int num = new Random().nextInt(this.testBox.size());paper.add(testPaper.get(num));//将随机得到的题目添加到试卷中 且试卷无重复}return new ArrayList<Question>(paper);//利用构造方法 返回ArrayList类型}}
public class Teacher {//老师改卷public int checkAns(ArrayList<Question> testPaper,String[] stuAnswer){int score = 0;for(int i = 0;i<stuAnswer.length;i++){if(testPaper.get(i).getAnswer().equalsIgnoreCase(stuAnswer[i])){score +=100/testPaper.size();}}return score;}}
主方法:
public static void main(String[] args) {TestSystem ts = new TestSystem();System.out.println("欢迎进入考试系统,系统正在加载中,请稍后!");try {Thread.sleep(3000);} catch (Exception e) {e.printStackTrace();}System.out.println("系统加载完毕,考试即将开始,请在系统内输入您的姓名:");Scanner input = new Scanner(System.in);String stuName = input.nextLine();System.out.println("请输入您的学号");String stuNum = input.nextLine();Student stu = new Student(stuName,stuNum);ts.login(stu);try {Thread.sleep(3000);} catch (Exception e) {e.printStackTrace();}ArrayList<Question> testPaper = ts.getTestPaper();Teacher t = new Teacher();int score = t.checkAns(testPaper,stu.answer(testPaper));System.out.println("考试结束,老师正在改卷,请留在座位上等待!");try {Thread.sleep(3000);} catch (Exception e) {e.printStackTrace();}System.out.println("改卷结束!您本次成绩为"+score);}
