敲码进程0.0% 2022-04-13 23:49 采纳率: 100%
浏览 42
已结题

编写一个Java考试小程序出错

img


结果总是显示为得继续努力,并未做出判断,是不是类的定义出了问题,还是check方法的设置或者是判断的算法有问题
附题目

img

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();
    }
    }


  • 写回答

2条回答 默认 最新

  • zcrazy胡说八道 2022-04-14 09:54
    关注
    
    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条)

报告相同问题?

问题事件

  • 系统已结题 4月22日
  • 已采纳回答 4月14日
  • 创建了问题 4月13日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效