结果总是显示为得继续努力,并未做出判断,是不是类的定义出了问题,还是check方法的设置或者是判断的算法有问题
附题目
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[]answers;
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(answers, answer[i]);
if (rest[i]<0)
{ System.out.println("还得努力呀!");
break;}
else if(rest[answer.length]>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);
Character.toUpperCase(z[i]);}
A.check(z);
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++)
{z[i]=y.charAt(i);
Character.toUpperCase(g[i]);}
B.check(g);
B.print();
sc.close();
}
}