Java实现考试系统
作者:山木嘉 时间:2023-11-18 04:15:03
本文实例为大家分享了Java实现考试系统的具体代码,供大家参考,具体内容如下
说明
这里的考试系统是指由学生,老师以及考试机构成的,学生通过用户名,密码登录考试机,考试机从题库中随机抽取一定数量的题目组合成试卷,学生对试卷进行作答,最后由老师对试卷进行批改,并给出成绩。
运行环境
IDEA
JDK 1.7.0_75
目录
代码
代码中都有一定的注释,所以不再另外说明。写代码时不是一整个类写完再去写下一个类的,而是写完一个类中的基本功能然后到下一个类,边写边测试这个功能能不能用,好用了再写下一个功能,所以有些类可能写的时候顺序有些难懂。
这里将问题作为单独的一个类
package ExamSystem;
public class Question {
private String title;//题干
private String answer;//正确答案
public Question(String title,String answer){
this.title=title;
this.answer=answer;
}
public String getTitle() {
return this.title;
}
public String getAnswer(){
return this.answer;
}
//要重写equals方法以及hashCode方法,才能在set集合中保证题目不重复
@Override
public boolean equals(Object obj) {
if(this==obj)
return true;
if(obj instanceof Question){
Question anotherQuestion=(Question)obj;
//比较题干是否相同
if(this.getTitle().equals(anotherQuestion.getTitle()))
return false;
}
return true;
}
@Override
public int hashCode() {
return this.getTitle().hashCode();
}
}
再看看考试机类
package ExamSystem;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Random;
/*
考试机,其中存储着题库,为了防止题库中的题重复,选用set集合存储,记得Question类重写hashCode()和equals()方法
*/
public class ExamMachine {
//记录学生账号和密码
private HashMap<String,String> userBank=new HashMap<String,String>();
{
userBank.put("张三","111");
userBank.put("李四","222");
userBank.put("王五","333");
}
//登录方法
public boolean login(String userName,String passWord){
String realPassWord=userBank.get(userName);
if(realPassWord!=null && realPassWord.equals(passWord))
return true;
return false;
}
//用Set集合作为题库
private HashSet<Question> questionBank=new HashSet<Question>();
//将题目加入题库
{
questionBank.add(new Question("下面哪个是合法的标识符?\n\tA.#_pound\n\tB._underscore\n\tC.5interstate\n\tD.class","B"));
questionBank.add(new Question("下面哪个单词是java语言的关键字?\n\tA.sizeof\n\tB.abstract\n\tC.null\n\tD.String","B"));
questionBank.add(new Question("Java中main()函数的返回值是什么?\n\tA.String\n\tB.int\n\tC.char\n\tD.void","D"));
questionBank.add(new Question("下面哪个是main()函数的合法参数?\n\tA.char args[]\n\tB.char args[][]\n\tC.String args[]\n\tD.String args","C"));
questionBank.add(new Question("以下哪个方法用于定义线程的执行体?\n\tA.start()\n\tB.init()\n\tC.run()\n\tD.main()","C"));
questionBank.add(new Question("以下哪个约束符可用于定义成员常量?\n\tA.static\n\tB.final\n\tC.abstract \n\tD.No modifier can be used","B"));
questionBank.add(new Question("提供Java存取数据库能力的包是?\n\tA.java.sql\n\tB.java.awt\n\tC.java.lang\n\tD.java.swing","A"));
questionBank.add(new Question("对象的特征在类中表示为变量,称为类的?\n\tA.对象\n\tB.属性\n\tC.方法\n\tD.数据类型","B"));
questionBank.add(new Question("下面哪个不能让线程停止执行?\n\tA.sleep();\n\tB.stop();\n\tC.wait();\n\tD.notify();","D"));
questionBank.add(new Question("下面哪个不是String类提供的合法的方法?\n\tA.equals(String)\n\tB.trim()\n\tC.append()\n\tD.indexOf()","C"));
}
//随机生成试卷---ArrayList存储
public ArrayList<Question> getPaper(int questionNum){
//定义一个set来存储试卷中的题目,防止题目重复
HashSet<Question> paper=new HashSet<Question>();
//从题库中随机取题目作为试卷,为了让题库有序,先将题库放进一个List集合,通过list的有序性取题目
ArrayList<Question> questionBank=new ArrayList<Question>(this.questionBank);
//随机生成数字,用来取题库对应中的题目
while(paper.size()<questionNum){
int index=new Random().nextInt(this.questionBank.size());
paper.add(questionBank.get(index));
}
return new ArrayList<Question>(paper);
}
}
学生类
package ExamSystem;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Scanner;
public class Student {
private String userName;
private String passWord;
public String getUserName() {
return userName;
}
public String getPassWord(){
return passWord;
}
public Student(String userName,String passWord){
this.userName=userName;
this.passWord=passWord;
}
//学生考试的方法,需要试卷,做完后返回答案
public String[] exam(ArrayList<Question> paper){
Scanner input=new Scanner(System.in);
String[] answers=new String[paper.size()];
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 ExamSystem;
import java.util.ArrayList;
/*
**老师负责批改试卷
*/
public class Teacher {
//老师批改试卷需要一张有考试答案的试卷,批改完给出分数
public int checkPaper(ArrayList<Question> paper,String[] answers){
System.out.println("改卷需要时间,请等待");
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;
}
}
主类
package ExamSystem;
import java.util.ArrayList;
import java.util.Scanner;
public class TestMain {
public static void main(String[] args){
int questionNum=5;//试卷的题目数量
ExamMachine machine=new ExamMachine();//创建考试机
ArrayList paper=machine.getPaper(questionNum);//随机生成试卷
Teacher teacher=new Teacher();
Scanner input=new Scanner(System.in);
System.out.println("欢迎进入考试系统,请输入用户名:");
String userName=input.nextLine();
System.out.println("请输入密码:");
String passWord=input.nextLine();
if(machine.login(userName,passWord)){
Student student=new Student(userName,passWord);
System.out.println("登录成功!"+student.getUserName()+",考试马上开始,请答题:");
String[] answers=student.exam(paper);
int score=teacher.checkPaper(paper,answers);
System.out.println(student.getUserName()+"的成绩为:"+score);
}else{
System.out.println("用户名或密码错误");
}
}
}
运行结果
来源:https://blog.csdn.net/hansmu/article/details/100560732
标签:java,考试系统
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android稳定性:可远程配置化的Looper兜底框架
2022-02-18 09:02:23
Android仿支付宝中余额宝的数字动画效果
2021-10-17 08:32:54
![](https://img.aspxhome.com/file/2023/1/137721_0s.gif)
Android拼图游戏 玩转从基础到应用手势变化
2021-08-24 02:41:59
![](https://img.aspxhome.com/file/2023/7/95967_0s.gif)
spring boot线上日志级别动态调整的配置步骤
2022-09-19 01:57:45
![](https://img.aspxhome.com/file/2023/2/94312_0s.png)
android studio生成aar包并在其他工程引用aar包的方法
2021-07-23 21:28:30
![](https://img.aspxhome.com/file/2023/2/130302_0s.jpg)
SpringBoot整合Elasticsearch游标查询的示例代码(scroll)
2022-02-11 02:02:13
浅析Spring Security登录验证流程源码
2023-03-22 01:46:44
![](https://img.aspxhome.com/file/2023/7/72287_0s.jpg)
Android ListView 和ScroolView 出现onmeasure空指针的解决办法
2021-11-25 18:19:03
Java String类的性质与比较
2021-07-29 16:48:37
![](https://img.aspxhome.com/file/2023/2/91732_0s.jpg)
Java实现简单的斗地主游戏
2023-03-13 10:42:57
![](https://img.aspxhome.com/file/2023/1/100661_0s.jpg)
java使用异或实现变量互换和异或加密解密示例
2023-10-19 16:53:38
javaWeb使用servlet搭建服务器入门
2023-11-21 04:47:45
Android Jetpack库剖析之LiveData组件篇
2022-08-31 02:08:13
spring cloud gateway 限流的实现与原理
2023-04-10 16:47:56
![](https://img.aspxhome.com/file/2023/8/73068_0s.png)
Java日常练习题,每天进步一点点(7)
2023-11-27 00:34:58
![](https://img.aspxhome.com/file/2023/8/59978_0s.png)
C#编程实现取整和取余的方法
2023-09-09 22:19:26
Android自定义有限制区域的图例角度自识别涂鸦工具类完结篇
2022-10-22 23:05:13
![](https://img.aspxhome.com/file/2023/9/138509_0s.jpg)
MyBatis查询结果resultType返回值类型的说明
2021-10-25 21:37:39
![](https://img.aspxhome.com/file/2023/9/79169_0s.jpg)
c# 单例模式的实现
2023-06-12 01:16:44
![](https://img.aspxhome.com/file/2023/4/80124_0s.png)
基于C#实现一个简单的FTP操作工具
2023-05-12 11:50:58