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

关于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 workstation加载centos进入emergency模式,查看日志报警如图,没有XFS,怎样解决呢?
  • ¥20 求各位解释一道区间DP
  • ¥15 应用商店如何检测在架应用内容是否违规?
  • ¥15 Ubuntu系统配置PX4
  • ¥50 nw.js调用activex
  • ¥15 数据库获取信息反馈出错,直接查询了ref字段并且还使用了User文档的_id而不是自己的
  • ¥15 将安全信息用到以下对象时发生以下错误:c:dumpstack.log.tmp 另一个程序正在使用此文件,因此无法访问
  • ¥15 速度位置规划实现精确定位的问题
  • ¥15 MAC虚拟机(win11)USB插上后无串口com,无法烧录
  • ¥15 代码问题:df = pd.read_excel('c:\User\18343\Desktop\wpsdata.xlxs')路径读不到