使用数组制作随机测验应用

时间:2016-01-20 22:34:39

标签: java android

我正在尝试使用数组制作随机测验应用,所以我想保持重复。你能提出一些我能做到的方法吗?我已经为我所拥有的内容添加了以下代码。我不确定我需要做什么。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.quiz);
    QuestionMaker();
}

这个函数会提出一系列问题:

//funtion makes the load of the question

public void QuestionMaker(){
    QText=(TextView) findViewById(R.id.textView_questions);
    Qgroup=(RadioGroup)findViewById(R.id.Group_radio);
    btnNext=(Button)findViewById(R.id.button_next);

    final ArrayList<String> distractorList = new ArrayList<String>();
    Collections.addAll(distractorList, Options);


//first time of filling the form

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


//clicking the radio button this happen


    Qgroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            makefinish++;
            QuestionAndAnswer qna = quiz.get(currentQuestion);
            qna.selectedAnswer = ((RadioButton) group.findViewById(checkedId)).getText().toString();

            if (qna.selectedAnswer == qna.answer) {
                correctcounter++;
                //correct message

            }
            if (!(qna.selectedAnswer == qna.answer)) {
//wrong answer message
            }

}

//function that makes the filling

public void fillInQuestion() {
    QuestionAndAnswer qna = quiz.get(currentQuestion);`enter code here`
    QText.setText(qna.question);

    int count = Qgroup.getChildCount();
    for (int i = 0; i < count; i++)
        ((RadioButton) Qgroup.getChildAt(i)).setText(qna.allAnswers.get(i));


}

1 个答案:

答案 0 :(得分:1)

有几种方法可以做到这一点。这是一个:

您可以通过随机生成0到数组中元素数之间的数字来随机选择一个问题。之后,您可以在屏幕上显示该问题。用户回答问题后,您要刷新屏幕并加载其他问题。此外,您应该创建另一个数组,该数组将包含已生成的数字列表,并检查问题编号以确保您之前没有问过该问题。

回答有关重复的问题:在用户输入第一个问题的答案后,更改文本并回答选择。这个逻辑也可以循环或某种形式。

相关问题