学生 进入 考试系统 考试 需要输入 名字 和 学号
    老师 修改 学生的 试卷
    考试系统中 有十道题 每次学生考试 会从题库中 随机抽取五道题
    考试前系统会检索学生信息 信息正确 才能开始考试

    1. public class Question {
    2. //面向对象的编程思想 将一道题目看成对象
    3. //属性 题目 答案
    4. private String tittle;
    5. private String answer;
    6. public Question(String tittle,String answer){
    7. this.tittle = tittle;
    8. this.answer = answer;
    9. }
    10. //重写equals方法
    11. public boolean equals(Object obj){
    12. if(this==obj){
    13. return true;
    14. }
    15. if(obj instanceof Question){
    16. Question anotherQuestion = (Question) obj;
    17. //按照?截取题目
    18. String[] question1 = this.tittle.split("?");
    19. String[] question2 = anotherQuestion.tittle.split("?");
    20. if(question1[0].equals(question2[0])){
    21. return true;
    22. }
    23. }
    24. return false;
    25. }
    26. //重写hashCode方法
    27. public int hashCode(){
    28. String[] question = this.tittle.split("?");
    29. return question[0].hashCode();
    30. }
    31. public String getTittle(){
    32. return this.tittle;
    33. }
    34. public String getAnswer(){
    35. return this.answer;
    36. }
    37. }
    1. public class Student {
    2. private String name;
    3. private String num;
    4. public Student(String name, String num) {
    5. this.name = name;
    6. this.num = num;
    7. }
    8. public String getName() {
    9. return this.name;
    10. }
    11. public String getNum() {
    12. return this.num;
    13. }
    14. //学生答题
    15. public String[] answer(ArrayList<Question> testPaper){
    16. String[] stuAnswer = new String[testPaper.size()];
    17. Scanner input = new Scanner(System.in);
    18. for(int i = 0;i<testPaper.size();i++){
    19. System.out.println((i+1)+"."+testPaper.get(i).getTittle());
    20. System.out.println("请输入您的答案:");
    21. stuAnswer[i] = input.nextLine();
    22. }
    23. return stuAnswer;
    24. }
    25. }
    1. public class TestSystem {
    2. //考试系统
    3. //属性---题库 题库中存有很多Question对象 每一个对象都是一道题目
    4. //可以考虑Set集合 无序无重复 这样重复的题目会拒绝录入
    5. //所以需要重写equals方法和hashCode方法
    6. private HashMap<String,String> stuBOx = new HashMap<>();//记录学生信息
    7. private HashSet<Question> testBox = new HashSet<Question>();
    8. {
    9. stuBOx.put("小兰","123");
    10. stuBOx.put("小明","456");
    11. stuBOx.put("小程","789");
    12. stuBOx.put("小红","000");
    13. }
    14. {
    15. testBox.add(new Question("以下哪个不是java的基本类型?\n\tA.short\n\tB.boolean\n\tC.String\n\tD.char","C"));//C
    16. testBox.add(new Question("以下哪个是java的基本类型?\n\tA.Short\n\tB.Boolean\n\tC.String\n\tD.char","D"));//D
    17. testBox.add(new Question("以下哪个数最大?\n\tA.5\n\tB.6\n\tC.7\n\tD.8","D"));//D
    18. testBox.add(new Question("以下哪个数最小?\n\tA.5\n\tB.6\n\tC.7\n\tD.8","A"));//A
    19. testBox.add(new Question("以下哪个是java的引用类型?\n\tA.String\n\tB.boolean\n\tC.int\n\tD.char","A"));//A
    20. testBox.add(new Question("以下哪个不是java的引用类型?\n\tA.String\n\tB.Integer\n\tC.Boolean\n\tD.char","D"));//D
    21. testBox.add(new Question("以下哪个不是java的包装类?\n\tA.Short\n\tB.Boolean\n\tC.String\n\tD.Character","C"));//C
    22. testBox.add(new Question("以下哪个是java的包装类?\n\tA.short\n\tB.Boolean\n\tC.String\n\tD.char","B"));//B
    23. testBox.add(new Question("以下哪个是java的权限修饰符?\n\tA.protected\n\tB.static\n\tC.final\n\tD.abstract","A"));//A
    24. testBox.add(new Question("以下哪个是java的特征修饰符?\n\tA.public\n\tB.final\n\tC.protected\n\tD.private","B"));//B
    25. }
    26. //若用户名或密码错误 重新输入
    27. private Student loginAgain(){
    28. System.out.println("用户名或密码错误,请重新输入:");
    29. Scanner input = new Scanner(System.in);
    30. String stuName = input.nextLine();
    31. System.out.println("请输入您的学号");
    32. String stuNum = input.nextLine();
    33. Student stu = new Student(stuName,stuNum);
    34. return stu;
    35. }
    36. //判断学生姓名与学号 和 系统信息是否一致
    37. public void login(Student stu){
    38. String realPassword = stuBOx.get(stu.getName());//判断这个人是不是真的存在 如果不存在为null
    39. if(realPassword!=null && realPassword.equals(stu.getNum())) {//如果人真的存在
    40. System.out.println("登录成功,考试即将开始,系统正在随机生成试卷,请梢等!");
    41. }else {
    42. login(loginAgain());
    43. }
    44. }
    45. //随机生成五道题目 返回值--->试卷
    46. //试卷应该也和题库一样 但是题目只有五道 试卷应该也是一个集合Set<Question>
    47. //给学生答题需要遍历一题一题来答 所以试卷可以是ArrayList<Question>
    48. public ArrayList<Question> getTestPaper(){
    49. //因为试卷的五道题应该是不重复的 所以不重复的考虑Set集合
    50. HashSet<Question> paper = new HashSet<>();//存储生成的试卷
    51. //先用Set集合存 然后最后再变成ArrayList
    52. ArrayList<Question> testPaper = new ArrayList<>(this.testBox);//存储试卷
    53. while (paper.size() != 5) {//当时卷存满五道题后 循环结束
    54. //产生一个随机的序号去找寻题目
    55. int num = new Random().nextInt(this.testBox.size());
    56. paper.add(testPaper.get(num));//将随机得到的题目添加到试卷中 且试卷无重复
    57. }
    58. return new ArrayList<Question>(paper);//利用构造方法 返回ArrayList类型
    59. }
    60. }
    1. public class Teacher {
    2. //老师改卷
    3. public int checkAns(ArrayList<Question> testPaper,String[] stuAnswer){
    4. int score = 0;
    5. for(int i = 0;i<stuAnswer.length;i++){
    6. if(testPaper.get(i).getAnswer().equalsIgnoreCase(stuAnswer[i])){
    7. score +=100/testPaper.size();
    8. }
    9. }
    10. return score;
    11. }
    12. }

    主方法:

    1. public static void main(String[] args) {
    2. TestSystem ts = new TestSystem();
    3. System.out.println("欢迎进入考试系统,系统正在加载中,请稍后!");
    4. try {
    5. Thread.sleep(3000);
    6. } catch (Exception e) {
    7. e.printStackTrace();
    8. }
    9. System.out.println("系统加载完毕,考试即将开始,请在系统内输入您的姓名:");
    10. Scanner input = new Scanner(System.in);
    11. String stuName = input.nextLine();
    12. System.out.println("请输入您的学号");
    13. String stuNum = input.nextLine();
    14. Student stu = new Student(stuName,stuNum);
    15. ts.login(stu);
    16. try {
    17. Thread.sleep(3000);
    18. } catch (Exception e) {
    19. e.printStackTrace();
    20. }
    21. ArrayList<Question> testPaper = ts.getTestPaper();
    22. Teacher t = new Teacher();
    23. int score = t.checkAns(testPaper,stu.answer(testPaper));
    24. System.out.println("考试结束,老师正在改卷,请留在座位上等待!");
    25. try {
    26. Thread.sleep(3000);
    27. } catch (Exception e) {
    28. e.printStackTrace();
    29. }
    30. System.out.println("改卷结束!您本次成绩为"+score);
    31. }