不知道如何编写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("请选择:");
}
}
我先给个还算正确的答案,你先参考一下:
package com.example;
import java.util.Arrays;
import java.util.Scanner;
class Question {
String text;
String[] options;
Question() {
}
Question(String text, String[] options) {
this.text = text;
this.options = options;
}
boolean check(char[] answers) {
return false;
}
void print() {
}
}
class MultiChoice extends Question {
char[] answer;
MultiChoice() {
}
MultiChoice(String text, String[] options, char[] answer) {
this.text = text;
this.options = options;
this.answer = answer;
System.out.println(text);
System.out.println(Arrays.toString(options));
}
boolean check(char[] answers) {
if (answers.length < answer.length || answers.length > answer.length)
System.out.println("还得努力呀!");
else {
int[] rest;
rest = new int[answer.length];
for (int i = 0; i < answer.length; i++) {
rest[i] = Arrays.binarySearch(answer, answer[i]);
if (rest[i] < 0) {
System.out.println("还得努力呀!");
break;
}
if (rest[answer.length-1] > 0)
System.out.println("恭喜,答对了!");
}
}
return true;
}
}
class SingleChoice extends Question {
char answer;
char[] answers;
SingleChoice() {
}
SingleChoice(String text, String[] options, char answer) {
this.text = text;
this.options = options;
this.answer = answer;
System.out.println(text);
System.out.println(Arrays.toString(options));
}
boolean check(char[] answers) {
this.answers = answers;
return answers[0] == answer;
}
void print() {
if (answers.length > 1 || answers.length == 0)
System.out.println("还得努力呀!");
else if (answer == answers[0])
System.out.println("恭喜,答对了!");
else
System.out.println("还得努力呀!");
}
}
public class Test {
public static void main(String args[]) {
MultiChoice A = new MultiChoice("三国演义中的三绝是谁?", new String[] { "A.曹操", "B.刘备", "C.关羽", "D.诸葛亮" },
new char[] { 'A', 'C', 'D' });
System.out.print("请选择:");
Scanner sc = new Scanner(System.in);
String x = sc.next();
char[] z;
z = new char[x.length()];
for (int i = 0; i < x.length(); i++) {
z[i] = x.charAt(i);
z[i] = Character.toUpperCase(z[i]);
}
A.check(z);
A.print();
SingleChoice B = new SingleChoice("最早向刘备推荐诸葛亮的是谁?", new String[] { "A.徐庶", "B.司马徽", "C.鲁肃", "D.关羽" }, 'A');
System.out.print("请选择:");
String y = sc.next();
char[] g;
g = new char[y.length()];
for (int i = 0; i < y.length(); i++) {
g[i] = y.charAt(i);
g[i] = Character.toUpperCase(g[i]);
}
B.check(g);
B.print();
sc.close();
}
}