考试机
需求
题[] 总题库 里面放的所有的题
题[] 临时题库 里面放的是随机抽出的3道题
学生[] 学生登录后可以答题 答题时循环展示 临时题库的题干和选项内容 学生作答后可以给出正确答案并且计算分值。
分析 oop
学生类
姓名 学号 登录名 密码
题类
题干 以下选项中谁最帅
选项 A.张三 B.张昊宇 C.李四 D.王五
答案 B
分 5
程序结构
考试机类
题[] 总题库
题[] 临时题库
学生[] 学生们
//构造方法中初始化上面的总题库和学生
//出题的方法
(随机三次从总题库中取出题放入到临时题库中)
//登陆的方法
//考试的方法
(调用 登录—>出题—>循环临时题库给出题目并且作答计算分值)
代码
1.题类
public class Ti {
public String tigan;//题干
public String xuanxiang;//选项
public String daan;//答案
public int score;//得分
public Ti(String tigan, String xuanxiang, String daan, int score) {
this.tigan = tigan;
this.xuanxiang = xuanxiang;
this.daan = daan;
this.score = score;
}
}
2.学生类
public class Student {
public String name;
public String id;
public String username;
public String password;
public Student(String name, String id, String username, String password) {
this.name = name;
this.id = id;
this.username = username;
this.password = password;
}
}
3.方法
import java.util.Scanner;
public class test {
public Ti[] tiku; //总题库
public Ti[] linshitiku=new Ti[3]; //临时题库
public Student[] stu; //学生
/**
* 初始化学生和总题库
*/
public test() {
Ti t1=new Ti(" 1+1=","A 1 B 2 C 3 D 4","B",10);
Ti t2=new Ti(" 1+3=?","A 1 B 2 C 3 D 4","D",10);
Ti t3=new Ti(" 2*2=?","A 1 B 2 C 3 D 4","D",10);
Ti t4=new Ti(" 2-3=?","A -1 B 2 C 3 D 4","A",10);
Ti t5=new Ti(" 2/2=?","A 1 B 2 C 3 D 4","A",10);
this.tiku= new Ti[]{t1, t2, t3, t4, t5}; //学生题库数组赋值部分
Student stu=new Student("张","201801","张昊宇","123456");
Student stu1=new Student("李","201802","李四","654321");
this.stu= new Student[]{stu, stu1};
}
/**
* 出题 随机三次从总题库中取出题放入到临时题库中。
*/
public void chuti() {
linshitiku = new Ti[3];
int index = 0;
for (int i = 0; i < 3; i++) {
//random会给出随机小数,这里需要去选择数组的位置,所以需要强转
//ztk.length这个给的是一个区间
int xx = (int) (Math.random() * tiku.length);
//测试在数组中选取哪个位置
// System.out.println((int) (Math.random() * ztk.length));
//如果重复怎么办
Ti t = tiku[xx];
boolean b = true;
for (int j = 0; j < linshitiku.length; j++) {
if (linshitiku[j] == t) {
i--; //外面在此循环
b = false;
break;
}
}
if (b == true) {
linshitiku[index++] = tiku[xx];
}
}
}
/**
* 登录
* @return 登录对象
*/
public Student login(){
Student s=null;
Scanner sc=new Scanner(System.in);
System.out.print("用户名:");
String username=sc.next();
System.out.print("密 码:");
String password=sc.next();
for (int i = 0; i <stu.length ; i++) {
if (stu[i].username.equals(username)&&stu[i].password.equals(password)){
s=stu[i];
System.out.println("登录成功");
break;
}
}
return s;
}
/**
* 考试方法调用 登录--> 出题 --> 循环临时题库 给出题目 并且作答 计算分值
*/
public void kaoshi(){
Student s=login();
if (s!=null) {
chuti(); //调完出题方法之后 lstk 中才会有内容
int dui=0;
int cuo=0;
int zong=30;
int score=0;
Scanner sc=new Scanner(System.in);
System.out.println("----------------考试开始------------------");
for (int i = 0; i <linshitiku.length ; i++) {
System.out.println(linshitiku[i].tigan);
System.out.println(linshitiku[i].xuanxiang);
//输入答案并判断正确与否
System.out.print("请输入的答案");
String da=sc.next();
//equalsIgnoreCase判断输入的是否在给定的组合中,并且不受大小写影响
if (linshitiku[i].daan.equalsIgnoreCase(da)){
System.out.println("回答正确");
score+=linshitiku[i].score;
dui++;
}else{
cuo++;
System.out.println("回答错误");
}
System.out.println("------------------------------------");
}
System.out.println("----------------考试结束------------------");
System.out.println("本次测试共三题(共30分),答对"+dui+"题,打错"+cuo+","+s.name+"总成绩"+score);
System.out.println("----------------考试结果------------------");
}else {
System.out.println("登录失败!");
}
}
public static void main(String[] args) {
test t=new test();
t.kaoshi();
}
}