zoukankan      html  css  js  c++  java
  • 考试答案对应

    完成考题类(Question),单选题(SingleChoice)和多选题(MoreChoice)是其子类。
    要求:
    1.Question包含题干属性(text),选项:String[] options
    Question 包含检测标准答案的方法 boolean check(char[] answers)
    参数 answers 用户提供的答案(注意:单选题只有一个答案)
    2.MoreChoice和SingleChoice是Question类的子类
    MoreChoice包含属性
    多选标准答案:char[] answers
    SingleChoice包含属性
    单选标准答案:char answer
    3.在MoreChoice实现参数为(String text,String [] options,char[]
    answers)的构造方法
    参数text题干
    参数options选项
    参数answers多选标准答案(正确选项的序号)
    4.在SingleChoice 实现参数为(String text,Stirng[] options,char
    answer)的构造方法
    参数text题干
    参数options选项
    参数answer标准答案(正确选项的序号)

     1 package Question;
     2 /**
     3  * 考题类,抽象类
     4  * @author Administrator
     5  *
     6  */
     7 public abstract class Question {
     8     private String text;//题干
     9     private String[]options;//选项
    10     protected Question(){}
    11     protected Question(String text, String[] options) {
    12         super();
    13         this.text = text;
    14         this.options = options;
    15     }
    16     public String getText() {
    17         return text;
    18     }
    19     public void setText(String text) {
    20         this.text = text;
    21     }
    22     public String[] getOptions() {
    23         return options;
    24     }
    25     public void setOptions(String[] options) {
    26         this.options = options;
    27     }
    28     /**
    29      * 打印题目信息
    30      */
    31     public void printQuestion(){
    32         //输出题干
    33         System.out.println(text);
    34         //输出选项
    35         for(int i=0;i<options.length;i++){
    36             System.out.println((char)('A'+i)+"."+options[i]);
    37         }
    38     }
    39     /**
    40      * 抽象方法,判断答案是否正确
    41      * @param answers
    42      * @return
    43      */
    44     protected abstract boolean check(String[]answers); 
    45 }
     1 package Question;
     2 /**
     3  * 单选题,继承考题类
     4  * @author Administrator
     5  *
     6  */
     7 public class SingleChoice extends Question {
     8     public String answers;
     9     public SingleChoice(String text,String[]options,String answers){
    10         super(text,options);
    11         this.answers=answers;
    12     }
    13     public String getAnswers() {
    14         return answers;
    15     }
    16     public void setAnswers(String answers) {
    17         this.answers = answers;
    18     }
    19     @Override
    20     protected boolean check(String[] userAns) {
    21         return answers.equals(userAns[0]);
    22     }
    23 }
     1 package Question;
     2 
     3 import java.util.Arrays;
     4 
     5 /**
     6  * 多选题,继承考题类
     7  * @author Administrator
     8  *
     9  */
    10 public class MoreChoice extends Question {
    11     public String[] answers;//标准答案
    12     
    13     public MoreChoice(String text,String []options,String[] answers) {
    14         super(text,options);//调用父类构造方法
    15         this.answers = answers;
    16     }
    17 
    18     public String[] getAnswers() {
    19         return answers;
    20     }
    21 
    22     public void setAnswers(String[] answers) {
    23         this.answers = answers;
    24     }
    25     @Override//重写标识
    26     protected boolean check(String[]userAns) {
    27         Arrays.sort(userAns);
    28         return Arrays.equals(answers, userAns);
    29     }
    30 }
     1 package Question;
     2 
     3 import java.util.Scanner;
     4 public class TestQuestion {
     5 
     6     public static void main(String[] args) {
     7         // 存放所有的考题
     8         Question[] list = new Question[5];
     9         list[0] = new SingleChoice("今天天气怎么样?", new String[] { "很好", "非常好",
    10                 "不好", "下雨" }, "D");
    11         list[1] = new SingleChoice("学习Java感觉怎么样?", new String[] { "很爽", "非常爽",
    12                 "一般", "你敢说不爽" }, "A");
    13         list[2] = new SingleChoice("以下哪个是Java的数据类型?", new String[] { "bool",
    14                 "number", "varchar", "int" }, "D");
    15         list[3] = new MoreChoice("Java面向对象三大特征是?", new String[] { "封装", "继承",
    16                 "多态", "私有化" }, new String[] { "A", "B", "C" });
    17         list[4] = new MoreChoice("Java语言的优点?", new String[] { "面向对象", "跨平台",
    18                 "弱类型", "脚本语言" }, new String[] { "A", "B" });
    19 
    20         Scanner input = new Scanner(System.in);
    21         // 输出考试题目 增强for循环
    22         for (Question question : list) {
    23             if (question != null) {
    24                 question.printQuestion();
    25                 System.out.print("请输入正确答案:");
    26                 String ansStr = input.nextLine();// 读取一行
    27                 String[] userAns = ansStr.split(" ");
    28                 if (question.check(userAns)) {// 判断用户答案
    29                     System.out.println("恭喜你回答正确!");
    30                 } else {
    31                     System.out.println("很遗憾回答错误!");
    32                 }
    33             }
    34         }
    35     }
    36 }
  • 相关阅读:
    如何下载网络图片资源
    经典排序之快速排序(含红黑树)
    经典排序之归并排序
    node微信公众号开发---自动回复
    koa2的文件上传
    async await的用法
    Generator yield语法和 co模块
    CentOS 7 下安装 Nginx
    windows下nginx的安装及使用方法入门
    linux下nodejs安装以及如何更新到最新的版本
  • 原文地址:https://www.cnblogs.com/SUN99bk/p/10476519.html
Copyright © 2011-2022 走看看