简单考试系统(只包含单选,多选)的实现

---Option.java

---Question.java

    ----SingleChoice.java

    ----MultiChoice.java

---sQuestionDemo.java 

以下是具体内容:

     Option.java:

 1 public class Option {
2 //名词都写成类
3 private char id; //A B C
4 private String text;//答案选项内容
5
6 public Option(){}
7
8 public Option (char id,String text){
9 this.id=id;
10 this.text=text;
11 }
12
13
14 public void setId(char id) {
15 this.id = id;
16 }
17 public char getId(){
18 return id;
19 }
20 public String getText() {
21 return text;
22 }
23 public void setText(String text) {
24 this.text = text;
25 }
26 }

Question.java

 1 public abstract class Question  {
2 //题号
3 protected int id;
4 //题干
5 protected String text;
6
7 public int getId() {
8 return id;
9 }
10 public void setId(int id) {
11 this.id = id;
12 }
13 public String getText() {
14 return text;
15 }
16 public void setText(String text) {
17 this.text = text;
18 }
19
20 //构造方法
21 public Question(){}
22 public Question(int id,String text){
23 this.id=id;
24 this.text=text;
25 }
26
27 //check方法
28 public abstract boolean check(char[] answer);
29 //show方法
30 public abstract void show();
31 }

SingleChoice.java

 1 public class SingleChoice extends Question {
2 /** 题干*/
3 //private String text;
4 /**选项*/
5 private Option[] options;
6 /**标准答案*/
7 private char answer;
8
9 // public String getText(){
10 // return text;
11 // }
12 // public void setText(String text){
13 // this.text=text;
14 // }
15 public Option[] getOptions(){
16 return options;
17 }
18 public SingleChoice(){}
19 public SingleChoice(int id,String text,Option[] options,char answer){
20 super(id,text);
21 //this.text=text;
22 this.options=options;
23 this.answer=answer;
24 }
25
26 public boolean check(char[] answer) {
27 // TODO Auto-generated method stub
28 //return super.check(answer);
29 return answer[0]==this.answer;
30 }
31 public void show() {
32 // TODO Auto-generated method stub
33 System.out.println(id+" "+text);
34 for(int i=0;i<options.length;i++){
35 Option o=options[i];
36 System.out.print(o.getId()+"."+o.getText()+"\t");
37 }
38 System.out.println();
39 }
40 }

MultiChoice.java

 1 import java.util.Arrays;
2
3 public class MultiChoice extends Question {
4
5 //private String text;
6 private Option[] options;
7 private char[] answers;
8
9 public MultiChoice(){}
10 public MultiChoice(int id,String text,Option[] options,char[] ansers){
11 super(id,text);
12 this.options=options;
13 this.answers=ansers;
14 }
15
16 public Option[] getOptions() {
17 return options;
18 }
19 public void setOptions(Option[] options) {
20 this.options = options;
21 }
22 public char[] getAnswers() {
23 return answers;
24 }
25 public void setAnswes(char[] answers) {
26 this.answers = answers;
27 }
28
29
30 public boolean check(char[] answer) {
31 // TODO Auto-generated method stub
32 // if(this.answers.length!=answer.length){
33 // return false;
34 // }
35 return Arrays.equals(this.answers, answer);//比较两个数组值一样。。
36 }
37 public void show() {
38 // TODO Auto-generated method stub
39 System.out.println(id+" "+text);
40 for(int i=0;i<options.length;i++){
41 Option o=options[i];
42 System.out.print(o.getId()+"."+o.getText()+"\t");
43 }
44 System.out.println();
45
46 //super.show();
47 }
48
49 }

QuestionDemo.java 

 1 import java.util.Scanner;
2
3 public class QuestionDemo {
4
5 /**
6 * @param args
7 */
8 public static void main(String[] args) {
9 // TODO Auto-generated method stub
10 Question[] questionare=new Question[2];
11 questionare[0]=new SingleChoice(
12 1,
13 "著名的和尚是谁?",
14 new Option[]{
15 new Option('A',"李平"),
16 new Option('B',"小僧"),
17 new Option('C',"大业"),
18 new Option('D',"小臂")
19 },
20 //new char[]{'A','B','C'}
21 'B'
22 );
23 questionare[1]=new MultiChoice(
24 2,
25 "著名的雷人酵母是哪几位?",
26 new Option[]{
27 new Option('A',"李平"),
28 new Option('B',"小僧"),
29 new Option('C',"大业"),
30 new Option('D',"小臂")
31 },
32 new char[]{'A','B','C'}
33 );
34 Scanner console = new Scanner(System.in);
35 for(int i=0;i<questionare.length;i++)
36 {
37 Question q= questionare[i];
38 //显示问题
39 q.show();
40 //读取问题
41 System.out.println("输入答案:");
42 char []answer= console.nextLine().toCharArray();
43 //比较答案与标准答案
44 boolean pass = q.check(answer);
45 //输出大体结果
46 if(pass){
47 System.out.println("牛啊!");
48 }else{
49 System.out.println("猪啊!!");
50 }
51 }
52 }
53
54 }
原文地址:https://www.cnblogs.com/superjt/p/2117953.html