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

关于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 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程