#定义考题类(Question)及其子类
完成考题类(Question),单选题(SingleChoice)和多选题(MultiChoice)是其子类
要求:
1)Question包含题干属性(text)
2)Question包含检测标准答案的方法 boolean check(int[] answers)
参数answers:用户提供的答案(注意:单选题只有一个答案)
3)单选题(SingleChoice)和多选
- List item
题(MultiChoice)是Question的子类
MultiChoice包含属性:
选项:String[] options
多选标准答案:int[] answers
SingleChoice包含属性:
选项:String[] options
单选标准答案:int answers
4)在MultiChoice实现参数为(String text,String[] options,int[] answers)的构造方法
参数text:题干
参数options:选项
参数answers:多选标准答案(正确选项的序号)
5)在SingleChoice实现参数为(String text,String[] options,int answers)的构造方法
参数text:题干
参数options:选项
参数answers:单选标准答案(正确选项的序号)
6)在SingleChoice和MultiChoice类中重写Question类中的check方法
方法中提供具体的检查用户答案的逻辑
1、定义父类,包含text属性和check方法
public class Question {//定义父类,考题类
private String text;
//题干,字符串用来存储题目
public String getText() {
//text的get方法
return text;
}
public void setText(String text) {
//text的set方法
this.text = text;
}
boolean check(int answers) {
//父类的check方法,用来子类继承并重写
return true;
}
}
2、单选题类包含选项和答案两个属性,还要重写check方法进行答案对比
public class SingleChoice extends Question{
public SingleChoice(String text,String[] options, int answers) {
//定义有参构造器,依次包含题干,选项,标准答案这些参数
super();
this.options = options;
this.answers = answers;
this.setText(text);//set方法调用父类text属性
}
private String[] options= {"1、爷爷","2、奥特曼","3、灭爸","4、变形金刚"};
private int answers;
//定义answers属性
public String[] getOptions() {
return options;
}
public void setOptions(String[] options) {
this.options = options;
}
public int getAnswers() {
return answers;
}
public void setAnswers(int answers) {
this.answers = answers;
}
public SingleChoice() {
super();
}
public boolean check(int answers) {
if(answers==1) {
//选项里面的答案是1,所以当输入的答案是1时,返回true
return true;
}else {
return false;//否则返回false
}
}
}
在这里插入代码片
3、多选题类也包含选项和答案两个属性,也要重写check方法进行答案对比
private String[] options= {"1、外公","2、外婆","3、葫芦娃","4、蝎子精"};
//定义并初始化options属性
private int[] answers=new int [2];
//定义并初始化answers属性
public String[] getOptions() {
return options;
}
public void setOptions(String[] options) {
this.options = options;
}
public int[] getAnswers() {
return answers;
}
public void setAnswers(int[] answers) {
this.answers = answers;
}
//自动生成set和get方法
public MultiChoice(String text,String[] options, int[] answers) {
//调用有参构造器,参数依次为题干、选项、答案
super();
this.options = options;
this.answers = answers;
this.setText(text);//使用set方法,调用父类私有属性
}
public MultiChoice() {
//当定义了有参的构造器之后,需要手动生成无参的构造器
super();
}
boolean chech(int[] answer) {
if((answers[0]==1 && answers[1]==2)||(answers[0]==2&&answer[1]==2)) {
//多选题的答案是两个,当答案是1 、2或者2、1时,都会判断答案正确
return true;
}
else return false;
}
}
4、最后使用Test类,创建各子类对象,使用check方法对比输入答案是否正确
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
SingleChoice s=new SingleChoice();
//创建单选题类的对象s
MultiChoice m=new MultiChoice();
// 创建多选题的对象m
s.setText("单选题:爸爸的爸爸叫什么?");
// 用set方法给父类text属性赋值
System.out.println(s.getText());
//输出单选题题干,准备答题
for(String a:s.getOptions()) {
//用增强for循环输出选项
System.out.println(a);
}
Scanner key=new Scanner(System.in);
//创建一个键盘扫描器用于输入答案
int num=key.nextInt();
if(s.check(num)) {//调用check方法判断输入的答案是否正确
System.out.println("回答正确");
}else {
System.out.println("回答错误");
}
m.setText("多选题:妈妈的父母叫什么");//使用set方法对text属性进行初始化
System.out.println(m.getText());//输入多选题的题干
for(String a:m.getOptions()) {
//使用增强for循环输出多选题选项
System.out.println(a);
}
int[] arr=m.getAnswers();//使用get方法获取私有属性Answers
arr[0]=key.nextInt();//对键盘扫描器输入的数值放进数组
arr[1]=key.nextInt();
if(m.chech(arr)) {//对多选题答案进行判断
System.out.println("回答正确");
}else {
System.out.println("回答错误");
}
key.close();
}
}