java练习题
Posted 星空6
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java练习题相关的知识,希望对你有一定的参考价值。
最终代码如下:
package kaoshi; import java.util.*; public class Machine{ //记录学生账号和密码; private HashMap<String,String> userB=new HashMap<String,String>(); { userB.put("admin","123"); userB.put("lisi","456"); userB.put("张三","oab"); } private HashSet<Question> qb=new HashSet<Question>(); { qb.add(new Question("以下选项哪个不是java基本数据类型?\\n A.String\\tB.char\\tC.int\\tD.double","A")); qb.add(new Question("以下选项哪个是java基本数据类型?\\n A.Int\\tB.Math\\tC.Random\\tD.Date","A")); qb.add(new Question("在java中负责对字节代码解释执行的是?\\n A.String\\tB.虚拟机\\tC.int\\tD.double","B")); qb.add(new Question("一个栈的输入序列为1 2 3 4 5,则下列中不可能是栈的输出序列是?\\n A.String\\tB.char\\tC.int\\tD.54132","D")); qb.add(new Question("对象的特征在类中表示为变量,成为类的?\\n A.String\\tB.char\\tC.属性\\tD.double","C")); qb.add(new Question("表达式(11+3*8)/4%3的值是?\\n A.String\\tB.0\\tC.2\\tD.double","C")); qb.add(new Question("表达式不可以作为循环条件的是?\\n A.i++\\tB.i>5\\tC.i==2\\tD.double","A")); qb.add(new Question("以下哪个不是合法的标识符?\\n A.STRING\\tB.X3\\tC.DE$f\\tD.void","D")); qb.add(new Question("下列说法正确的是?\\n A. JAVA程序的main方法必须写在类里面\\tB.X3\\tC.DE$f\\tD.void","A")); /* Iterator<Question> it=qb.iterator(); while(it.hasNext()){ System.out.println(it.next()); } */ }//利用块初始化HashSet集合内的题目对象; public HashMap<String,String> getUserB(){ return userB; } public ArrayList<Question> getPaper(){ HashSet<Question> paper=new HashSet<Question>();//试卷 Random rd=new Random(); ArrayList<Question> qbb=new ArrayList<Question>(this.qb); while(paper.size()!=5){//试卷的题目数量; int value=rd.nextInt(qb.size()); paper.add(qbb.get(value)); //System.out.println(paper.size()+" "+value); } return new ArrayList<Question>(paper); } public boolean login(String name,String password){ String realps=this.getUserB().get(name); if(realps!=null && realps.equals(password)){ return true; } return false; /* Set<String> names=this.getUserB().keySet();//获取用户名集合; Iterator<String> it=names.iterator(); boolean rs=false; while(it.hasNext()){ String ii=it.next(); //System.out.println(ii); if(name.equals(ii)){ if(password.equals(this.getUserB().get(name))){ rs=true; } break; } } return rs; */ } }
package kaoshi; 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 title; } public String getAnswer(){ return answer; } //重写方法 比较题干 public boolean equals(Object obj){ if(this==obj){ return true; } if(obj instanceof Question){ Question anotherQ=(Question)obj; if(this.title.equals(anotherQ.title)){ return true; } } return false; } public int hashCode(){ return this.title.hashCode(); } }
package kaoshi; import java.util.*; public class Student{ private String username; private String password; public Student(String username,String password){ this.username=username; this.password=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 qs=paper.get(i);//题干+答案 //System.out.println(qs); System.out.println((i+1)+"."+qs.getTitle()); System.out.println("请您开始答题,A B C D"); answers[i]=input.nextLine(); } return answers; } public String getUsername(){ return username; } public String getPassword(){ return password; } }
package kaoshi; import java.util.*; public class Teacher{ public int piJuan(ArrayList<Question> a,String[] answer){//试卷(含答案),学生的答案 int total=0; for(int i=0;i<a.size();i++){ if(a.get(i).getAnswer().equalsIgnoreCase(answer[i])){//不区分大小写 System.out.println("第"+(i+1)+"题回答正确!"); total+=(100/a.size()); } } return total; } }
package kaoshi; import java.util.*; public class TestMain{ public static void main(String[] args){ /* String question="1.如何哪个选项不是java基本数据类型?\\n A.String\\tB.char\\tC.int\\tD.double"; System.out.println(question); String answer="A"; //如何将题目和答案放一起 //String[],String,HashMap,Array,对象 Question q=new Question("1.如何哪个选项不是java基本数据类型?\\n A.String\\tB.char\\tC.int\\tD.double","A"); //Question q=new Question("1.如何哪个选项不是java基本数据类型?\\n A.String\\tB.char\\tC.int\\tD.double","A"); System.out.println(q.getTitle()); System.out.println(q.getAnswer()); */ Machine m=new Machine(); Scanner input=new Scanner(System.in);//输入 System.out.println("欢迎进入考试系统"); System.out.println("请输入用户名:"); String name=input.nextLine(); System.out.println("请输入密码:"); String ps=input.nextLine(); Student s=new Student(name,ps);//学生 if(m.login(s.getUsername(),s.getPassword())){ System.out.println("登录成功!\\n"+s.getUsername()+":开始考试,请不要作弊!"); ArrayList<Question> t=m.getPaper(); //试卷; System.out.println("考试机正在随机的生成试卷,请耐心等待一会"); try{ Thread.sleep(5000);//睡5s }catch(Exception e){ e.printStackTrace(); } String[] sresult=s.exam(t);//学生答案 Teacher th=new Teacher();//老师 int result=th.piJuan(t,sresult); System.out.println(s.getUsername()+"此次考试最终成绩:"+result+"分"); }else{ System.out.println("账号或密码错误!"); } } }
以上是关于java练习题的主要内容,如果未能解决你的问题,请参考以下文章