不知道如何编写check()方法来达到判断对错,check()方法中的语句return answers[0]==answer;放在判断条件前面就显示出错,放在后面,不论答案是什么都判断为正确。
import java.util.Arrays;
import java.util.Scanner;
public class TestQuestion {
public static void main(String[] args) {
SingleQuestion s = new SingleQuestion("最早向刘备推荐诸葛亮的是谁?",new String[]{"A.徐庶","B.司马徽","C.鲁肃","D.关羽"},'A');
s.show();
Scanner answers= new Scanner(System.in);
String x=answers.next();
char[] answer = {'A'};
s.check(answer);
char[] g = new char[x.length()];
MultiQuestion m = new MultiQuestion("三国演义中的三绝是谁?",new String[]{"A.曹操","B.刘备","C.关羽","D.诸葛亮"},new char[]{'A','C','D'});
m.show();
Scanner sc= new Scanner(System.in);
String y=sc.next();
char[] answers1 = {'A','B','D'};
m.check(answers1);
char[] h = new char[y.length()];
}
}
class Question {
protected String text;//测试题
public Question() { }//无参构造
public Question(String text) {//有参构造
this.text = text;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
//展示方法
public void show() {
}
//检测答案的方法
public boolean check(String[] answers) {
return true;
}
}
class SingleQuestion extends Question{
char[] answers;
char answer;
String[] options;
String text;
public SingleQuestion() {
}
public SingleQuestion(String text, String[] options, char answer) {
this.text = text;
this.options = options;
this.answer = answer;
}
public boolean check(char[] answers) {
this.answers=answers;
return answers[0]==answer;
if(answers.length>1||answers.length==0)
System.out.println("还得努力呀!");
else if(answer==answers[0])
System.out.println("恭喜,答对了!");
else
System.out.println("还得努力呀!");
}
public void show() {
System.out.println(text);
System.out.println(Arrays.toString(options));
System.out.println("请选择:");
}
}
class MultiQuestion extends Question{
char[] answers;
String[] options;
public MultiQuestion() {
}
public MultiQuestion(String text, String[] options, char[] answers) {
this.text = text;
this.options = options;
this.answers = answers;
}
public char[] check(char[] answers) {
return this.answers=answers ;
}
public void show() {
System.out.println(text);
System.out.println(Arrays.toString(options));
System.out.print("请选择:");
}
}