【问题标题】:Android random multiple choice quiz: how to identify correct answerAndroid随机多项选择测验:如何识别正确答案
【发布时间】:2012-08-11 22:57:46
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: android random arrays multiple-choice


    【解决方案1】:

    我建议创建一个名为 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,这样用户可以直观地选择答案。我还假设会有prevnext 按钮,它们会调整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++;
    

    这是我的总体想法。代码是一个完整的想法,所以它会编译。当然,您需要添加一个“下一步”按钮来查看不同的问题。但这足以让您看到一种在保持问题和答案井井有条的同时随机化问题和答案的方法。

    【讨论】:

    • 我添加了一些代码,如果文本视图是随机填充的,您能否详细说明如何保存用户的选择?
    • 做一个 QuestionAndAnswer 类,其中包含问题的正确答案,但有一个静态的“干扰因素”列表,可以在所有 QuestionAndAnswer 对象之间共享。此外,还有一个名为 usersChoice 的变量来存储用户实际选择的答案。您可以通过在 QuestionAndAnswer 类中实现 OnClickListener 并为每个 testView 执行 textView.setOnClickListener(this) 来找到实际选择的那个。
    • questionTextView.setText(qna.question);由于 questionTextView 对象在 questionTextView = (TextView) findViewById(R.id.question) 处设置为 null,因此在 fillInQuestion() 函数中抛出空指针异常;你能解释一下为什么吗?
    • @neel 是的,您的布局没有@+id/question 的视图。 findViewById() 只能定位当前绘制的视图。确保main.xml 拥有您想要的视图。
    【解决方案2】:

    这里有一个示例,你可以试试。这是一个数据模型,喜欢为问答事物保存东西。

    <data-map>
        <question id="1">
            <ask>How many questions are asked on Android category daily? </ask>
            <answer-map>
                <option id="1">100 </option>
                <option id="2">111 </option>
                <option id="3">148 </option>
                <option id="4">217 </option>
            </answer-map>
            <correct id="3" />
        </question>
    
    
        <question id="2">
            <ask>Which band does John Lenon belong to? </ask>
            <answer-map>
                <option id="1">The Carpenters </option>
                <option id="2">The Beatles </option>
                <option id="3">Take That </option>
                <option id="4">Queen </option>
            </answer-map>
            <correct id="2" />
        </question>
    
    </data-map>
    

    好的,所以每次显示问题时,您都有所有要回答的选项,以及每个问题的正确答案。 只需创建一个适当的数据结构来保存它们。 无论如何,只是一个样本,不是一个完美的,但如果你是这些东西的新手,请尝试一下^^!

    【讨论】:

    • 如果不总是在同一个地方,我将如何识别正确答案?我有 java 代码在四个选项之一中随机显示正确答案,问题顺序也是随机的。另外,您能否详细说明“正确的数据结构”?谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-13
    • 2020-07-18
    • 1970-01-01
    • 2018-11-30
    • 1970-01-01
    相关资源
    最近更新 更多