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();
}
}
import java.util.Arrays;
import java.util.Locale;
import java.util.Scanner;
abstract class Question{
private final String text;
private final String []options;
public Question(String text,String []options){
this.text=text;
this.options=options;
}
public String getText(){
return this.text;
}
public String[] getOptions(){
return this.options;
}
abstract void check(char []answers);
}
class MultiChoice extends Question{
private final char[] answer;
MultiChoice(String text,String []options,char []answer){
super(text,options);
this.answer=answer;
System.out.println(text);
System.out.println(Arrays.toString(options));
}
public void check(char[] answers) {
if (answers.length < answer.length || answers.length > answer.length)
System.out.println("还得努力呀!");
else {
Arrays.sort(answer);
String answerStr = String.valueOf(answer);
Arrays.sort(answers);
String answersStr = String.valueOf(answers);
if(answerStr.equals(answersStr)){
System.out.println("恭喜,答对了!");
} else{
System.out.println("还得努力呀!");
}
}
}
}
class SingleChoice extends Question{
char answer;
SingleChoice(String text,String []options,char answer){
super(text,options);
this.answer=answer;
System.out.println(text);
System.out.println(Arrays.toString(options));
}
public void check(char[] answers) {
if(answers.length != 1)
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();
A.check(x.toUpperCase(Locale.ROOT).toCharArray());
SingleChoice B=new SingleChoice("最早向刘备推荐诸葛亮的是谁?",new String[]{"A.徐庶","B.司马徽","C.鲁肃","D.关羽"},'A');
System.out.print("请选择:");
String y=sc.next();
B.check(y.toUpperCase(Locale.ROOT).toCharArray());
sc.close();
}
}
1.从编程思维看,建议在基础知识上多巩固,尽快形成学习体系
2.从代码逻辑看,还停留在顺序编程阶段,建议尽快适应面向对象,可以让思路更清晰,代码更简洁
3.最好不要省略代码,比如if后的大括号,阅读体验不好,并不会让人觉得很酷
问题:
{z[i]=y.charAt(i); 第87行,你直接复制过来的时候没有改数组,应该是{g[i]=y.charAt(i);