Java

1.考试的题目该如何存储?
    自己描述一个类--->一个题目类型
    有两个属性---题干  真实答案
    public class Question{
     private String title;
     private Stirng answer;
    }
 2.有几个实体类(3个)  类的关系?
    考试机
         属性---题库 存储好多Question类型的对象
                 set无重复
         方法---随机抽题目 试卷
    学生
         方法---考试(试卷)
    老师
         方法---批卷子(学生最终的选项,真实的试卷)
 3.具体添加每一个类中的成员描述
    涉及到如何选择容器来存储的问题
 4.具体的执行验证

package tool_class_exam;
//一个题目的类型
public class Question {
    private String title;//存放题目
    private String answer;//存放答案

    //通过对象来存储题库的题目和答案
    public Question(String title,String answer){
        this.answer = answer;
        this.title = title;
    }
    //用来获取题库中的题目
    public String getTitle(){
        return this.title;
    }
    //用来获取题库中的答案
    public String getAnswer(){
        return this.answer;
    }
    //无重复规则
    public boolean equals(Object object){
        if(this == object){
            return true;
        }
        if(object instanceof Question){   //instanceof运算符用于测试构造函数的prototype属性是否出现在对象的原型链中的任何位置
            Question anotherQuestion = (Question)object;
            if(this.title.equals(anotherQuestion.title)){
                return true;
            }
        }
        return false;
    }

    public int hashCode(){
        return this.title.hashCode();
    }
}

package tool_class_exam;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Random;

public class ExamMachine {
    //存储登录信息
    private HashMap<String,String> hashMap= new HashMap<String, String>();
    {
        hashMap.put("123","123");
        hashMap.put("1234","1234");
        hashMap.put("12345","12345");
    }

    //生成题库 Set类型如果扩充题库 会将重复的题去除
    //判断题目是否重复,只需判断题干,需要重写Question类中的 equals与hashCode
    private HashSet<Question> questionBank = new HashSet<Question>();
    {
        questionBank.add(new Question("以下选项哪个是Java基本数据类型?
	A.String
	B.Integer
	C.boolean
	D.Math","C"));
        questionBank.add(new Question("以下选项哪个不是Java基本数据类型?
	A.String
	B.int
	C.boolean
	D.double","A"));
        questionBank.add(new Question("以下选项哪个是Java引用数据类型?
	A.String
	B.int
	C.boolean
	D.char","A"));
        questionBank.add(new Question("以下选项哪个不是Java引用数据类型?
	A.String
	B.Integer
	C.boolean
	D.Math","C"));
        questionBank.add(new Question("以下选项哪个是java.util包中的类?
	A.String
	B.Integer
	C.Scanner
	D.Math","C"));
        questionBank.add(new Question("以下选项哪个不是java.util包中的类?
	A.Date
	B.Integer
	C.Calendar
	D.Random","B"));
        questionBank.add(new Question("以下选项哪个不是String类中的方法?
	A.compareTo
	B.append
	C.substring
	D.concat","B"));
        questionBank.add(new Question("以下选项哪个是String类中方法?
	A.append
	B.delete
	C.insert
	D.concat","D"));
        questionBank.add(new Question("以下选项哪个不是接口中属性的修饰符?
	A.public
	B.static
	C.final
	D.abstract","D"));
        questionBank.add(new Question("以下选项哪个不是Set集合的方法?
	A.set
	B.add
	C.remove
	D.iterator","A"));
    }
    //生成一份试卷 给学生,因为学生需要遍历题目,所以题目最终用ArrayList类型存储
     public ArrayList<Question> getPaper(){
         System.out.println("开始抽取试卷,请耐心等候");
         try {
             Thread.sleep(5000);
         }catch(Exception e){
             e.printStackTrace();
         }

         //随机抽取试卷的时候,试卷的题目应该是不重复,所以用set集合先存储题目,然后存入ArryList中
         HashSet<Question> paper = new HashSet<Question>();
         //抽题需要随机产生一个序号,但Set集合没有序号,所以将题库转换为有序的 然后抽题
         ArrayList<Question> questionBank = new ArrayList<Question>(this.questionBank);//直接通过this调用对象 构建
         //产生随机的5道题
         //因为不知道循环几次 所以用while
        while(paper.size()!=5){
            int index = new Random().nextInt(this.questionBank.size());//产生一个[0,10)的随机数 用来抽题
            paper.add(questionBank.get(index));
        }
         return new ArrayList<Question>(paper);
     }
        //登录方法
     public boolean login(String user,String password){
         String realPassword = this.hashMap.get(user);//.get返回指定键映射到的值,如果此映射不包含键的映射,则返回空值。
if(realPassword!=null && realPassword.equals(password)){
             return true;
         }
         return false;
     }
    
/* public boolean login(String user,String password){
if(this.hashMap.containsKey(user) && this.hashMap.containsValue(password)){
return true;
}
return false;
}*/

}
package tool_class_exam; import java.util.ArrayList; import java.util.Scanner; public class Student { private String user; private String password; public Student(String user,String password){ this.password = password; this.user = user; } public String getUser(){ return this.user; } public String getPassword(){ return this.password; } //学生需要一份试卷 来答题 返回一个学生的答案 使用数组 public String[] exam(ArrayList<Question> paper){ String [] answer = new String[paper.size()];//创建一个数组存储学生的答案 Scanner input = new Scanner(System.in); for(int i=0; i<answer.length; i++){ Question question = paper.get(i);//获取题目和答案 System.out.println(i+1+". "+question.getTitle());//给出题目 System.out.println("请输入你的答案"); answer[i] = input.nextLine(); } return answer; } } package tool_class_exam; import java.util.ArrayList; public class Teacher { //返回分数 传入学生的答案 和 题目答案 public int checkPaper(ArrayList<Question> paper,String [] answer){ int score =0; for(int i=0;i<paper.size();i++){ Question question = paper.get(i); if(question.getAnswer().equalsIgnoreCase(answer[i])){ score+=(100/paper.size());// 100/paper.size(); } } return score; } } package tool_class_exam; import java.util.ArrayList; import java.util.Scanner; public class TestMain { public static void main(String[] args){ ExamMachine examMachine = new ExamMachine(); Scanner input = new Scanner(System.in); System.out.println("请输入账号:"); String user = input.nextLine(); System.out.println("请输入密码:"); String password = input.nextLine(); //登录 if (examMachine.login(user,password)) { Student student = new Student(user,password); System.out.println("欢迎"+student.getUser()+"进入考试系统"); ArrayList<Question> paper = examMachine.getPaper(); String[] answers = student.exam(paper);//学生考试 System.out.println("稍后公布成绩"); try { Thread.sleep(5000); }catch(Exception e){ e.printStackTrace(); } Teacher teacher = new Teacher(); int score = teacher.checkPaper(paper, answers); System.out.println(student.getUser()+"的最终的成绩为:" + score); }else { System.out.println("登录失败"); } } }
原文地址:https://www.cnblogs.com/yyanghang/p/11207210.html