Android的随机选择题测验:如何识别正确的答案 [英] Android random multiple choice quiz: how to identify correct answer

查看:260
本文介绍了Android的随机选择题测验:如何识别正确的答案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建为Android随机选择题测验。我想从一个字符串数组显示随机抽题,从另一个字符串数组显示的四个选项中了相应的答案。其他3个选择将来自另一个字符串数组,将用于提供对所有问题的错误的答案,随机。

两个问题:
有没有更好的方法,使一个选择题测验这样吗?
-和-
当玩家选择一个答案,我怎么确定的答案来自哪个阵列?

这是code我使用随机化:

 的String [] =问题{//问题在这里//};
ArrayList的<串GT; questionList =新的ArrayList(Arrays.asList(问题));的String [] =回答{//这里的答案//};
ArrayList的<串GT; answerList =新的ArrayList(Arrays.asList(回答));的String [] =分心这里{// //分心};
ArrayList的<串GT; distractorList =新的ArrayList(Arrays.asList(牵引));INT I = 0;
随机R =新的随机();
公共无效随机化(){        TextView中字=(的TextView)findViewById(R.id.textView1);
        TextView的choice1 =(的TextView)findViewById(R.id.textView2);
        TextView中选择2 =(的TextView)findViewById(R.id.textView3);
        TextView的choice3 =(的TextView)findViewById(R.id.textView4);
        TextView的choice4 =(的TextView)findViewById(R.id.textView5);
        如果(ⅰ&下; question.length){
            剩余的int = r.nextInt(questionList.size());
            串Q = questionList.get(剩余);
            word.setText(Q);
            questionList.remove(剩余);
            字符串= answerList.get(剩余);
            INT时隙= r.nextInt(4);
            TextView的[] tvArray = {choice1,选择2,choice3,choice4};
            tvArray [槽] .setText(一);
            answerList.remove(剩余);
          //一个if这里/ else语句,以填补分心剩余的插槽中


解决方案

我建议创建一个名为QuestionAndAnswer新类。该类应持的问题,正确的答案,它也可以容纳任何自定义错误的答案和用户的选择。确切的实现是完全取决于你。

在您的活动有这个QuestionAndAnswer类的循环在列表中提出的问题并在完成后总结出的点的数组。

(如果你包括你已经尝试一下相关的code我能更具体。)


加入

这就是我下手:结果
(从你的code我猜 distractorList 包含您要显示的错误答案。)

 公共类QuestionAndAnswer {
    公开名单<串GT; allAnswers; //分心加上真正的答案
    公共字符串的答案;
    公共字符串的问题;
    公共字符串selectedAnswer;
    公众诠释selectedId = -1;    公共QuestionAndAnswer(字符串问题,字符串的答案,列表与LT;弦乐>干扰项){
        this.question =问题;
        this.answer =答案;
        allAnswers =新的ArrayList<串GT; (干扰项);        //真正的答案添加到错误的答案,并围绕它们洗牌
        allAnswers.add(答案);
        Collections.shuffle(allAnswers);
    }    公共布尔isCorrect(){
        返回answer.equals(selectedAnswer);
    }
}

有关我改变你的四答案TextViews成RadioGroup中的活动,这样一来,用户可以直观地选择一个答案。我还以为会有 $ P $光伏接下来按钮,他们将调整 INT currentQuestion 和呼叫 fillInQuestion()

 公共类示例扩展活动{
    RadioGroup中answerRadioGroup;
    INT currentQuestion = 0;
    TextView的questionTextView;
    清单< QuestionAndAnswer>测验=新的ArrayList< QuestionAndAnswer>();    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);        questionTextView =(的TextView)findViewById(R.id.question);
        answerRadioGroup =(RadioGroup中)findViewById(R.id.answers);        //设置一个监听器,保存所选答案
        answerRadioGroup.setOnCheckedChangeListener(新OnCheckedChangeListener(){
            @覆盖
            公共无效onCheckedChanged(RadioGroup中组,诠释checkedId){
                如果(checkedId -1个){
                    QuestionAndAnswer QNA = quiz.get(currentQuestion);
                    qna.selectedAnswer =((单选)group.findViewById(checkedId))的getText()的toString()。
                    qna.selectedId = checkedId;
                }
            }
        });        的String [] =问题{//问题在这里//};
        的String [] =回答{//这里的答案//};
        的String [] =分心这里{// //分心};
        ArrayList的<串GT; distractorList = Arrays.asList(牵引);        / *我认为有问题,每3分心,他们在distractorList组织像这样:
         *Q1牵引1,Q1牵引2,Q1牵引3,
         *Q2牵引1,Q2牵引2,Q2牵引3,
         *等
         *
         *如果问题是:天空的颜色,你会看到干扰项:
         *红色,绿色,紫罗兰
         * /
        INT长度= question.length;
        的for(int i = 0; I<长度;我+ +)
            quiz.add(新QuestionAndAnswer(问题[I]中,回答[I],distractorList.subList(ⅰ* 3,第(i + 1)* 3)));
        Collections.shuffle(测验);        fillInQuestion();
    }    公共无效fillInQuestion(){
        QuestionAndAnswer QNA = quiz.get(currentQuestion);
        questionTextView.setText(qna.question);        //设置所有的答案中的单选按钮
        诠释计数= answerRadioGroup.getChildCount();
        的for(int i = 0; I<计数;我++)
            ((单选)answerRadioGroup.getChildAt(I))的setText(qna.allAnswers.get(I))。        //如果存在,否则清除previous问题的选择还原选定的答案
        如果(qna.selectedId -1个)
            answerRadioGroup.check(qna.selectedId);
        其他
            answerRadioGroup.clearCheck();
    }
}

您可能已经注意到,QuestionAndAnswer有isCorrect()方法,当是时候年级测验可以算正确答案是这样的:

  INT正确= 0;
对于(QuestionAndAnswer问题:测验)
    如果(question.isCorrect())
        正确++;

这是我的总体思路。在code是一个完整的思想,所以它会编译。当然,你要添加一个下一步按钮,看到了不同的问题。但是,这是足以让你看到你的随机问题和答案,同时保持他们有组织的方法之一。

I'm trying to create a random multiple choice quiz for android. I want to display a random question from a stringarray, with the corresponding answer from another string array showing up in one of the four options. The other three options will come from another string array, which will be used provide the "wrong" answers for all the questions, randomly.

Two questions: Is there a better way to make a multiple choice quiz like this? -and- When the player selects an answer, how do I identify which array the answer came from?

This is the code I'm using to randomize:

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

解决方案

I suggest creating a new class called QuestionAndAnswer. The class should hold the question and the correct answer, it could also hold any customized wrong answers and the user's choice. The exact implementation is entirely up to you.

In your Activity have an Array of this QuestionAndAnswer class to cycle through the list asking the questions and tally up the points when done.

(I could be more specific if you include the relevant code of what you have tried.)


Addition

This is what I would start with:
(From your code I'm guessing the distractorList contains the false answers that you want to display.)

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

For the Activity I changed your four answer TextViews into a RadioGroup, this way the user can intuitively select an answer. I also assume that there will be prev and next buttons, they will adjust int currentQuestion and call 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();
    }
}

You may have noticed that QuestionAndAnswer has an isCorrect() method, when it is time to grade the quiz you can count the correct answers like this:

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

This is my general idea. The code is a complete thought, so it will compile. Of course, you'll want to add a "next" Button to see the different questions. But this is enough for you to see one way to randomize your questions and answers while keeping them organized.

这篇关于Android的随机选择题测验:如何识别正确的答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆