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

JAVA考试小程序的编写出错

运行结果没有做出判断,怎么输入判断结果
都为“还得努力啊!”请问是不是做判断的算法写错了,还是check方法设置的问题还是其他参数传递的原因
题目为

img


运行结果

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();
    }
    }
  • 写回答

3条回答 默认 最新

  • 溪风沐雪 2022-04-13 23:23
    关注

    看了一下逻辑上问题不大,但是后边变量弄错了,我图里标了一下,如果改了还不行的话你贴一下代码吧,看图片太累了

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • 嘉祐-小萝卜算子 2022-04-13 23:17
    关注

    第二个问题你用了变量y 但是还是使用的第一个问题的答案x

    评论
  • _知道者丶 2022-04-14 09:33
    关注

    把你报错的信息放出来。你提问题人家csdn都把模板给你弄好了

    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 4月22日
  • 已采纳回答 4月14日
  • 修改了问题 4月14日
  • 修改了问题 4月13日
  • 展开全部

悬赏问题

  • ¥15 c++图Djikstra算法求最短路径
  • ¥15 Linux操作系统中的,管道通信问题
  • ¥15 请问这张multisim图的原理是什么,这是一个交通灯,是课程要求,明天要进行解析,但是我们组没一个人会,所以急要,今天要
  • ¥15 ansible tower 卡住
  • ¥15 等间距平面螺旋天线方程式
  • ¥15 通过链接访问,显示514或不是私密连接
  • ¥100 系统自动弹窗,键盘一接上就会
  • ¥50 股票交易系统设计(sql语言)
  • ¥15 调制识别中这几个数据集的文献分别是什么?
  • ¥15 使用c语言对日志文件处理