小任务--考试系统

小任务
设计一个学生 考试机 老师关系
考试机存储题目,随机生成考试试卷,并且验证登陆信息
学生登陆考试机,并且做题
老师负责批改卷子得出学生的最终成绩

题目类
属性:题干,答案
方法:重写equals和hashCode方法使相同的题存不进来

学生类:
属性:账号,密码
方法:考试(试卷) 返回作题的所有答案

考试机:
属性:题库,存储学生的账号和密码
方法:随机产生5道题,组成一套(试卷) 验证登陆

老师:
方法:批卷(学生的最终选项,真实的试卷)
下面给代码

package dome;

import java.util.ArrayList;
import java.util.Scanner;

/*
 * 考试系统
 */
public class Main {

	public static void main(String[] args) {
	   RobotExam robot = new RobotExam();   //创建考试机
	   Scanner cin = new Scanner(System.in);
	   System.out.println("               欢迎进入考试系统          ");
	   System.out.println("考生登陆 ");
	   System.out.print("账号:");
	   String account = cin.nextLine();   //输入账号
	   System.out.print("密码:");
       String password = cin.nextLine();   //密码
       String results = robot.login(account, password);
       if(results == "登陆成功")
       {
    	   
    	   Student student = new Student(account,password);   //登陆成功就创建一个学生对象
    	   ArrayList<Problem> problem = robot.smoke();  //生成试卷
	       String[] result = student.exam(problem);   //进入考试 -----得到考试答案
	       int cord = new Teacher().correct(problem,result);   //创建老师类
	       System.out.println("你此次考试的分数为:"+cord);
       }
	}

}

package dome;

import java.util.ArrayList;
import java.util.Scanner;

/*
 * 学生类
 */
public class Student {
    private String account;
    private String password;
    public Student(String account,String password)
    {
    	this.account = account;
    	this.password = password;
    }
    //考试 需要返回学生的考试答案
    public String[] exam(ArrayList<Problem> problem)  //考试需要题目   
    {
    	String[] answer = new String[problem.size()];  //答案
    	Scanner cin = new Scanner(System.in);
    	for(int i=0;i<problem.size();i++)
    	{
    		System.out.println((i+1)+"."+problem.get(i).gettitle());  //输出题干
    		System.out.print("请输入答案:");
    		answer[i] = cin.nextLine();  //将答案存入
    	}
		return answer;  //最后返回
    }
}

package dome;

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

/*
 * 考试机
 */
public class RobotExam {
	private HashSet<Problem> bank = new HashSet<Problem>();  //题库
	private HashMap<String,String> student = new HashMap<String,String>();  //存入学生的账号和密码
	//代码块  创建考试机的时候就存入
	{
		student.put("王桥", "123");
		student.put("廉洁", "456");
		student.put("李国栋", "www");
	}
	//代码块  创建考试机的时候 先创建题库
	{
		bank.add(new Problem("JAVA所定义的版本中不包括下列哪一个?\n\tA.JAVA2 EE\n\tB.JAVA2 Card\n\tC.JAVA2 ME\n\tD.JAVA2 HE","D")); 
		bank.add(new Problem("为一个boolean类型变量赋值时,可以使用哪种方式?\n\tA.boolean = 1\n\tB. boolean a = (9 >= 10)\n\tC.boolean a=\"真\"\n\tD.boolean a = = false","B"));
		bank.add(new Problem("以下不是合法的标识符?\n\tA.STRING\n\tB.x3x\n\tC.void\n\tD.de$f","C"));
		bank.add(new Problem("表达式(11+3*8)/4%3的值是?\n\tA.31\n\tB.0\n\tC.1\n\tD.2","C"));
		bank.add(new Problem("下列表达式不可以作为循环条件?\n\tA.i++\n\tB.i>5\n\tC.bEqual = str.equals(\"q\")\n\tD.count == i","A"));
		bank.add(new Problem("下列值不为true的表达式有?\n\tA.\"john\" = = \"john\"\n\tB.\"john\".equals(\"john\")\n\tC.\"john\" = \"john\"\n\tD.\"john\".equals(new String(\"john\"))","C"));
		bank.add(new Problem("对象的特征在类中表示为变量,称为类的是?\n\tA.对象\n\tB.属性\n\tC.方法\n\tD.数据类型","B"));
		bank.add(new Problem("下面不是String类提供的合法的方法?\n\tA.equals(String)\n\tB.trim()\n\tC.append()\n\tD.indexOf()","C"));
		bank.add(new Problem("在JAVA的异常处理模型中,能单独和finally语句一起使用的块是?\n\tA.try\n\tB.catch\n\tC.throw\n\tD.throws","A"));
		bank.add(new Problem("Java中,如果类C是类B的子类,类B是类A的子类,那么下面描述正确的是?\n\tA.C不仅继承了B中的成员,同样也继承了A中的成员\n\tB.C只继承了B中的成员\n\tC.C只继承了A中的成员\n\tD.C不能继承A或B中的成员","A"));
	}
   //随机抽取5题目
	public ArrayList<Problem> smoke()
	{
        waity("正在生成试题中......","试题生成成功");  
		ArrayList<Problem> list = new ArrayList<Problem>(bank);  //先把题库放到list里
		HashSet<Problem> box = new HashSet<Problem>();  //装抽取的题目,以防抽到重复的题目
		Random random = new Random();  //创建随机函数对象
		int cnt;
		while(box.size()!=5)  //直到抽满5道为止
		{
			cnt = random.nextInt(bank.size());
			box.add(list.get(cnt));  //再从list里面抽取
		}
		ArrayList<Problem> problem = new ArrayList<Problem>(box);  //再把box里面的题放入problem里面,再返回回去
		return problem;
	}
	//登陆验证功能
	public String login(String account,String password)
	{
		String s1 = "登陆成功";
		String s2 = "账号或密码输入错误";
		String newpassword = student.get(account);
		if(newpassword != null && password.equals(newpassword))  //如果能找到该用户名并且密码相同
		{
			waity("正在验证用户中————————",s1);  
			return s1;
		}
		waity("正在验证用户中————————",s2);
		return s2;
	}
	//等待方法
	private void waity(String str,String strz)
	{
		
		try {
			System.out.print(str);
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			System.out.println(strz);
		}
	}
}

package dome;
/*
 * 问题
 */
public class Problem {
     private String title;   //题干
     private String answer;  //答案
     public Problem(String title,String answer)
     {
    	 this.title = title;
    	 this.answer = answer;
     }
     public String gettitle()   //得到题干
     {
		return title;
     }
     public String getanswer()   //得到答案
     {
		return answer;
     }
     public boolean equals(Object obj)  //重写equals方法
     {
    	if(this == obj)   //先比较两个对象的地址
    	{
    		return true;    //地址一样,肯定是相同的
    	}
    	if(obj instanceof Problem)  //再看看obj是否是Problem
    	{
    		Problem problem = (Problem) obj;  //转化
    		return this.title.equals(problem.title);
    	}
		return false;
     }
     public int hashCode()   //重写hashCode方法
     {
    	 return this.title.hashCode();
     }
}

package dome;

import java.util.ArrayList;

/*
 * 教师类
 */
public class Teacher {
	
   public int correct(ArrayList<Problem> problem,String[] result)  //批改试卷需要卷子和答案
   {
	   int cord = 0;
	   int cnt = problem.size();  //题目数
	   for(int i=0;i<cnt;i++)
	   {
		   if(problem.get(i).getanswer().equalsIgnoreCase(result[i]))  //如果正常和学生填写的答案相同  不管大小写
		   {
			   cord += 100/cnt;   
		   }
	   }
	   return cord;
   }
}

到此结束哈,如果发现有错误的请留言,谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值