天使牌海盗 2022-04-14 20:43 采纳率: 90.9%
浏览 118
已结题

关于Java的一个简单考试程序,出题与答题的交互

不知道如何编写check()方法来达到判断对错,check()方法中的语句return answers[0]==answer;放在判断条件前面就显示出错,放在后面,不论答案是什么都判断为正确。

img

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("请选择:");
 }

}

  • 写回答

1条回答 默认 最新

  • 溪风沐雪 2022-04-14 21:07
    关注

    我先给个还算正确的答案,你先参考一下:

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

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

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

悬赏问题

  • ¥15 MIMIC数据库安装问题
  • ¥60 基于JTag协议开发Fpga下载器上位机,哪位大🐂有偿指导?
  • ¥20 全书网Java爬取数据
  • ¥15 怎么获取红包封面的原始链接,并且获取红包封面序列号
  • ¥100 微信小程序跑脚本授权的问题
  • ¥60 为什么使用python对地震数据进行umap降维后,数据成图会出现不连续的现象
  • ¥100 房产抖音小程序苹果搜不到安卓可以付费悬赏
  • ¥15 STM32串口接收问题
  • ¥15 腾讯IOA系统怎么在文件夹里修改办公网络的连接
  • ¥15 安卓qpython向ksweb服务器post文件失败