1 package com.ceshi; 2 3 public class Question { 4 private int questionId; 5 private String question; 6 private String optionA; 7 private String optionB; 8 private String optionC; 9 private String optionD; 10 private int subject; //科目 1代表Java、2代表C#、3代表JSP 11 private String answer; 12 public Question() { 13 } 14 public Question(int questionId, String question, String optionA, String optionB, String optionC, String optionD, int subject, 15 String answer) { 16 this.questionId = questionId; 17 this.question = question; 18 this.optionA = optionA; 19 this.optionB = optionB; 20 this.optionC = optionC; 21 this.optionD = optionD; 22 this.subject=subject; 23 this.answer = answer; 24 } 25 public int getQuestionId() { 26 return questionId; 27 } 28 public void setQuestionId(int questionId) { 29 this.questionId = questionId; 30 } 31 public String getQuestion() { 32 return question; 33 } 34 public void setQuestion(String question) { 35 this.question = question; 36 } 37 public String getOptionA() { 38 return optionA; 39 } 40 public void setOptionA(String optionA) { 41 this.optionA = optionA; 42 } 43 public String getOptionB() { 44 return optionB; 45 } 46 public void setOptionB(String optionB) { 47 this.optionB = optionB; 48 } 49 public String getOptionC() { 50 return optionC; 51 } 52 public void setOptionC(String optionC) { 53 this.optionC = optionC; 54 } 55 public String getOptionD() { 56 return optionD; 57 } 58 public void setOptionD(String optionD) { 59 this.optionD = optionD; 60 } 61 public String getAnswer() { 62 return answer; 63 } 64 public void setAnswer(String answer) { 65 this.answer = answer; 66 } 67 public int getSubject() { 68 return subject; 69 } 70 public void setSubject(int subject) { 71 this.subject = subject; 72 } 73 74 @Override 75 public String toString() { 76 return this.questionId+"."+this.question+"\n"+this.optionA+"\n"+this.optionB+"\n"+this.optionC+"\n"+this.optionD+"\n\t答案:"+this.answer; 77 } 78 79 @Override 80 public boolean equals(Object obj) { 81 // TODO Auto-generated method stub 82 Question q=(Question)obj; 83 return q.getQuestion().equals(question); 84 } 85 86 87 }
1 package com.ceshi; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 7 public class QuestionMrg { 8 List<Question> list=new ArrayList<Question>(); 9 public void init() { 10 Question q1=new Question(01,"下面关于栈的描述中错误的是( )","A、栈是先进后出的线性表","B、栈只能顺序存储", 11 "C、栈具有记忆功能","D、对栈的插入弓删除操作中,不需要改变栈底指针",1,"B"); 12 Question q2=new Question(2,"在Java中,负责对字节代码解释执行的是( )","A.垃圾回收器","B.虚拟机", 13 "C.编译器","D.多线程机制",1,"C"); 14 Question q3=new Question(3,"在Visual Studio.Net窗口中,()窗口提供令人连接本地或远程计算机上数据库并进行管理的功能","A 工具箱","B 资源视图 ", 15 "C 解决方案资源管理器","D 服务器资源管理器",2,"D"); 16 Question q4=new Question(4,"XCOPY命令的()选项用来指示复制所有子目录,即使是空目录也要复制","A /q ","B /s", 17 "C /p ","D /e",2,"D"); 18 Question q5=new Question(5,"如果做动态网站开发,以下()可以作为服务器端脚本语言","A java ","B JSP", 19 "C Javascript ","D Html",3,"A"); 20 21 list.add(q1); 22 list.add(q2); 23 list.add(q3); 24 list.add(q4); 25 list.add(q5); 26 } 27 28 /** 29 * 查询所有试题 30 */ 31 public void queryQuestions() { 32 for (Question q : list) { 33 System.out.println(q); 34 } 35 } 36 37 /** 38 * 按科目查询 39 */ 40 public void queryQuestionsBySubject(int subject) { 41 for (Question q : list) { 42 if(q.getSubject()==subject) { 43 System.out.println(q); 44 } 45 } 46 } 47 48 /** 49 * 按题干模糊查询方法1 50 * @param question 51 */ 52 public void queryQuestionsByQuestion(String question) { 53 boolean isFind=false; 54 for (Question q : list) { 55 if(q.getQuestion().contains(question)) { 56 System.out.println(q); 57 isFind=true; 58 } 59 } 60 if(!isFind) { 61 System.out.println("你要查找的试题不存在!"); 62 } 63 } 64 65 /** 66 * 按题干模糊查询方法2 67 * @param question 68 */ 69 public void queryQusetionsByQusetion2(String question) { 70 boolean isFind=false; 71 for (Question q : list) { 72 if(q.getQuestion().indexOf(question)!=-1 ) { 73 System.out.println(q); 74 isFind=true; 75 } 76 } 77 if(!isFind) { 78 System.out.println("你要查找的试题不存在!"); 79 } 80 } 81 82 /** 83 * 添加试题 84 */ 85 public void addQuestion(Question q) { 86 if(list.size()==0) { 87 q.setQuestionId(01); 88 }else { 89 q.setQuestionId(list.get(list.size()-1).getQuestionId()+1); 90 } 91 list.add(q); 92 } 93 94 /** 95 * 删除试题方法1 96 * @param question 题干 97 */ 98 public void deleteQuestion(String question) { 99 for (Question q : list) { 100 if(q.getQuestion().equals(question)) { 101 list.remove(q); 102 System.out.println("删除成功!"); 103 return; 104 } 105 } 106 System.out.println("试题不存在!"); 107 } 108 109 110 /** 111 * 删除试题方法2 112 * @param question 对象 113 */ 114 public void deleteQuestion2(Question question) { 115 if(list.contains(question)) { 116 list.remove(question); 117 System.out.println("删除成功!"); 118 }else { 119 System.out.println("试题不存在!"); 120 } 121 } 122 }
1 package com.ceshi; 2 3 import java.util.Scanner; 4 5 public class QuestionMain { 6 public static void main(String[] args) { 7 Scanner input =new Scanner(System.in); 8 QuestionMrg qm=new QuestionMrg(); 9 qm.init(); 10 11 do { 12 System.out.print("欢迎使用试题管理系统\n请操作:1.列出所有试题 2.按科目查询 3.按题干模糊查询 4.添加试题 5.删除试题 6.退出系统 "); 13 int choose=input.nextInt(); 14 switch(choose) { 15 case 1: 16 qm.queryQuestions(); 17 break; 18 case 2: 19 System.out.print("请输入科目:(1、Java 2、C# 3、JSP)"); 20 int subject=input.nextInt(); 21 qm.queryQuestionsBySubject(subject); 22 break; 23 case 3: 24 System.out.print("请输入题干:"); 25 String question=input.next(); 26 // qm.queryQuestionsByQuestion(question); 27 qm.queryQusetionsByQusetion2(question); 28 break; 29 case 4: 30 Question q=new Question(); 31 System.out.print("请输入科目(1、Java 2、C# 3、JSP):"); 32 subject=input.nextInt(); 33 q.setSubject(subject); 34 System.out.println("请输入新的题干:"); 35 question=input.next(); 36 q.setQuestion(question); 37 System.out.println("请输入新的选项A:"); 38 String optionA=input.next(); 39 q.setOptionA(optionA); 40 System.out.println("请输入新的选项B:"); 41 String optionB=input.next(); 42 q.setOptionB(optionB); 43 System.out.println("请输入新的选项C:"); 44 String optionC=input.next(); 45 q.setOptionC(optionC); 46 System.out.println("请输入新的选项D:"); 47 String optionD=input.next(); 48 q.setOptionD(optionD); 49 System.out.println("请输入新的答案:"); 50 String answer=input.next(); 51 q.setAnswer(answer); 52 qm.addQuestion(q); 53 break; 54 case 5: 55 System.out.print("请输入需要删除的题干:"); 56 q=new Question(); 57 q.setQuestion(input.next()); 58 qm.deleteQuestion2(q); 59 break; 60 case 6: 61 System.out.println("欢迎再次使用!"); 62 return; 63 } 64 }while(true); 65 66 } 67 }