Android随机多项选择测验:如何识别正确答案

Android随机多项选择测验:如何识别正确答案,第1张

概述我正在尝试为android创建一个随机多项选择测验.我想从stringarray中显示一个随机问题,其中一个字符串数组的相应答案显示在四个选项之一中.其他三个选项将来自另一个字符串数组,它将用于随机提供所有问题的“错误”答案.两个问题:是否有更好的方法来进行这样的多项选择测验?-和-当

我正在尝试为android创建一个随机多项选择测验.我想从stringarray中显示一个随机问题,其中一个字符串数组的相应答案显示在四个选项之一中.其他三个选项将来自另一个字符串数组,它将用于随机提供所有问题的“错误”答案.

两个问题:
是否有更好的方法来进行这样的多项选择测验?
-和-
当玩家选择答案时,如何识别答案来自哪个阵列?

这是我用来随机化的代码:

String[] question = { //questions here// };  ArrayList<String> questionList = new ArrayList(Arrays.asList(question));  String[] answer = { //answers here// };  ArrayList<String> answerList = new ArrayList(Arrays.asList(answer));String[] distractor = { //distractors here// };  ArrayList<String> distractorList = new ArrayList(Arrays.asList(distractor));  int i = 0;  Random r = new Random();  public voID randomize() {        TextVIEw word = (TextVIEw) findVIEwByID(R.ID.textVIEw1);        TextVIEw choice1 = (TextVIEw) findVIEwByID(R.ID.textVIEw2);        TextVIEw choice2 = (TextVIEw) findVIEwByID(R.ID.textVIEw3);        TextVIEw choice3 = (TextVIEw) findVIEwByID(R.ID.textVIEw4);        TextVIEw choice4 = (TextVIEw) findVIEwByID(R.ID.textVIEw5);        if (i < question.length) {            int remaining = r.nextInt(questionList.size());            String q = questionList.get(remaining);            word.setText(q);            questionList.remove(remaining);            String a = answerList.get(remaining);            int slot = r.nextInt(4);            TextVIEw[] tvarray = { choice1, choice2, choice3, choice4 };            tvarray[slot].setText(a);            answerList.remove(remaining);          //an if/else statement here to fill the remaining slots with distractors

解决方法:

我建议创建一个名为QuestionAndAnswer的新类.课程应该持有问题和正确的答案,它还可以保留任何定制的错误答案和用户的选择.确切的实施完全取决于您.

在你的Activity中有一个这个QuestionAndAnswer类的数组,在列表中循环询问问题,并在完成后计算积分.

(如果你包含你所尝试的相关代码,我可能会更具体.)

加成

这就是我要开始的:
(从您的代码中我猜测distractorList包含您要显示的错误答案.)

public class QuestionAndAnswer {    public List<String> allAnswers; // distractors plus real answer    public String answer;    public String question;    public String selectedAnswer;    public int selectedID = -1;    public QuestionAndAnswer(String question, String answer, List<String> distractors) {        this.question = question;        this.answer = answer;        allAnswers = new ArrayList<String> (distractors);        // Add real answer to false answers and shuffle them around         allAnswers.add(answer);        Collections.shuffle(allAnswers);    }    public boolean isCorrect() {        return answer.equals(selectedAnswer);    }}

对于Activity,我将您的四个回答TextVIEws更改为RadioGroup,这样用户可以直观地选择答案.我还假设会有prev和next按钮,它们会调整int currentQuestion并调用fillinQuestion().

public class Example extends Activity {    RadioGroup answerRadioGroup;    int currentQuestion = 0;    TextVIEw questionTextVIEw;    List<QuestionAndAnswer> quiz = new ArrayList<QuestionAndAnswer>();    @OverrIDe    public voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.main);        questionTextVIEw = (TextVIEw) findVIEwByID(R.ID.question);        answerRadioGroup = (RadioGroup) findVIEwByID(R.ID.answers);        // Setup a Listener to save chosen answer        answerRadioGroup.setonCheckedchangelistener(new OnCheckedchangelistener() {            @OverrIDe            public voID onCheckedChanged(RadioGroup group, int checkedID) {                if(checkedID > -1) {                    QuestionAndAnswer qna = quiz.get(currentQuestion);                    qna.selectedAnswer = ((Radiobutton) group.findVIEwByID(checkedID)).getText().toString();                    qna.selectedID = checkedID;                }            }        });        String[] question = { //questions here// };          String[] answer = { //answers here// };          String[] distractor = { //distractors here// };          ArrayList<String> distractorList = Arrays.asList(distractor);          /* I assumed that there are 3 distractors per question and that they are organized in distractorList like so:         *   "q1 distractor 1", "q1 distractor 2", "q1 distractor 3",          *   "q2 distractor 1", "q2 distractor 2", "q2 distractor 3",         *   etc         *            * If the question is: "The color of the sky", you'd see distractors:         *   "red", "green", "violet"         */           int length = question.length;        for(int i = 0; i < length; i++)            quiz.add(new QuestionAndAnswer(question[i], answer[i], distractorList.subList(i * 3, (i + 1) * 3)));        Collections.shuffle(quiz);        fillinQuestion();    }    public voID fillinQuestion() {        QuestionAndAnswer qna = quiz.get(currentQuestion);        questionTextVIEw.setText(qna.question);        // Set all of the answers in the Radiobuttons         int count = answerRadioGroup.getChildCount();        for(int i = 0; i < count; i++)            ((Radiobutton) answerRadioGroup.getChildAt(i)).setText(qna.allAnswers.get(i));        // Restore selected answer if exists otherwise clear prevIoUs question's choice        if(qna.selectedID > -1)            answerRadioGroup.check(qna.selectedID);        else             answerRadioGroup.clearCheck();    }}

您可能已经注意到QuestionAndAnswer有一个isCorrect()方法,当需要对测验进行评分时,您可以计算正确的答案,如下所示:

int correct = 0;for(QuestionAndAnswer question : quiz)    if(question.isCorrect())        correct++;

这是我的一般想法.代码是一个完整的思想,所以它将编译.当然,您需要添加“下一个”按钮以查看不同的问题.但这足以让你看到一种方法来随机化你的问题和答案,同时保持他们的组织.

总结

以上是内存溢出为你收集整理的Android随机多项选择测验:如何识别正确答案全部内容,希望文章能够帮你解决Android随机多项选择测验:如何识别正确答案所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1098067.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存