package com.uncle.myutil.exam;
import java.util.ArrayList;
import java.util.Scanner;
public class TestMain {
public static void main(String[] args){
ExamMachine machine = new ExamMachine();
Scanner input = new Scanner(System.in);
System.out.println("欢迎进入考试系统,\n请输入用户名");
String username = input.nextLine();
System.out.println("请输入密码");
String password = input.nextLine();
Student student = new Student(username,password);
String result = machine.login(student.getUsername(),student.getPassword());
if(result.equals("登录成功")){
System.out.println("登录成功\n"+student.getUsername()+"开始考试啦,不要作弊,认真作答");
ArrayList<Question> paper = machine.getPaper();
String[] answers = student.exam(paper);
Teacher teacher = new Teacher();
int score = teacher.checkPaper(paper,answers);
System.out.println(student.getUsername()+"最终的成绩为:"+score);
}
}
}
package com.uncle.myutil.exam;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Random;
public class ExamMachine {
private HashMap<String,String> userBox = new HashMap<String,String>();
{
userBox.put("步尔斯特","123");
userBox.put("大叔","666");
userBox.put("Java","888");
}
private HashSet<Question> questionBank = new HashSet<Question>();
{
questionBank.add(new Question("以下选项哪个是Java基本数据类型?\n\tA.String\n\tB.Integer\n\tC.boolean\n\tD.Math","C"));
questionBank.add(new Question("以下选项哪个不是Java基本数据类型?\n\tA.String\n\tB.int\n\tC.boolean\n\tD.double","A"));
questionBank.add(new Question("以下选项哪个是Java引用数据类型?\n\tA.String\n\tB.int\n\tC.boolean\n\tD.char","A"));
questionBank.add(new Question("以下选项哪个不是Java引用数据类型?\n\tA.String\n\tB.Integer\n\tC.boolean\n\tD.Math","C"));
questionBank.add(new Question("以下选项哪个是java.util包中的类?\n\tA.String\n\tB.Integer\n\tC.Scanner\n\tD.Math","C"));
questionBank.add(new Question("以下选项哪个不是java.util包中的类?\n\tA.Date\n\tB.Integer\n\tC.Calendar\n\tD.Random","B"));
questionBank.add(new Question("以下选项哪个不是String类中的方法?\n\tA.compareTo\n\tB.append\n\tC.substring\n\tD.concat","B"));
questionBank.add(new Question("以下选项哪个是String类中方法?\n\tA.append\n\tB.delete\n\tC.insert\n\tD.concat","D"));
questionBank.add(new Question("以下选项哪个不是接口中属性的修饰符?\n\tA.public\n\tB.static\n\tC.final\n\tD.abstract","D"));
questionBank.add(new Question("以下选项哪个不是Set集合的方法?\n\tA.set\n\tB.add\n\tC.remove\n\tD.iterator","A"));
}
public ArrayList<Question> getPaper(){
System.out.println("考试机正在随机的生成试卷,请耐心等待。。。");
try {
Thread.sleep(5000);
}catch(Exception e){
e.printStackTrace();
}
HashSet<Question> paper = new HashSet<Question>();
ArrayList<Question> questionBank = new ArrayList<Question>(this.questionBank);
while(paper.size()!=5) {
int index = new Random().nextInt(this.questionBank.size());
paper.add(questionBank.get(index));
}
return new ArrayList<Question>(paper);
}
public String login(String username,String password){
String realPassword = this.userBox.get(username);
if(realPassword!=null && realPassword.equals(password)){
return "登录成功";
}
return "用户名或密码错误";
}
}
package com.uncle.myutil.exam;
public class Question {
private String title;
private String answer;
public Question(String title,String answer){
this.title=title;
this.answer=answer;
}
public boolean equals(Object obj){
if(this==obj){
return true;
}
if(obj instanceof Question){
Question anotherQuestion = (Question)obj;
if(this.title.equals(anotherQuestion.title)){
return true;
}
}
return false;
}
public int hashCode(){
return this.title.hashCode();
}
public String getTitle(){
return this.title;
}
public String getAnswer(){
return this.answer;
}
}
package com.uncle.myutil.exam;
import java.util.ArrayList;
import java.util.Scanner;
public class Student {
private String username;
private String password;
public Student(String username,String password){
this.username=username;
this.password=password;
}
public String getUsername(){
return this.username;
}
public String getPassword(){
return this.password;
}
public String[] exam(ArrayList<Question> paper){
String[] answers = new String[paper.size()];
Scanner input = new Scanner(System.in);
for(int i=0;i<paper.size();i++){
Question question = paper.get(i);
System.out.println((i+1)+"."+question.getTitle());
System.out.println("请输入您认为正确的选项?");
answers[i] = input.nextLine();
}
return answers;
}
}
package com.uncle.myutil.exam;
import java.util.ArrayList;
public class Teacher {
public int checkPaper(ArrayList<Question> paper, String[] answers){
System.out.println("老师正在批卷子,请耐心等待最终成绩。。。");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
int score = 0;
for(int i=0;i<paper.size();i++){
Question question = paper.get(i);
if(question.getAnswer().equalsIgnoreCase(answers[i])){
score+=(100/paper.size());
}
}
return score;
}
}