0

我需要一种方法来为 ArrayList 中的一个问题分配 3 个可能的答案,其中只有一个答案是正确的。

我是 Android 新手,如果您能提供帮助,我将不胜感激。

我在这里有一个问题课:

package com.example.quiz;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.widget.TextView;

public class Questions extends Activity {
    private ArrayList<String> questionsArray = new ArrayList<String>();


    public Questions() {
        addQuestion("Which Prophet was the first in Islam?");
        addQuestion("What is the purpose of life?");
        addQuestion("Who is the last Prophet in Islam?");

    }

    public void addQuestion(String question) {
        questionsArray.add(question);
    }

    public ArrayList<String> getQuestionsArray() {
        return questionsArray;
    }

    public void setQuestionsArray(ArrayList<String> questionsArray) {
        this.questionsArray = questionsArray;
    }
}
4

2 回答 2

0

Maps are useful when having to associate data with other data. In your case, you are trying to associate questions to arrays of answers for each question.

You can make a HashMap<String, ArrayList<String>> answers and retain the answers (the ArrayList<String>) for all questions (the String), then have another HashMap<String, String> correctAnswers where you retain the question (1st String) and it's associated correct answer (2nd String).

When validating the answer you would have to get the answer that the user has chosen and compare it to the correct answer like this:

if(correctAnswers.get(questionString).compareTo(chosenAnswerString))
{
    //correct answer!
}
else
{
    //incorrect answer
}

The method used to add a question along with its answers you would become:

[...]

private Map answers = new HashMap<String, ArrayList<Sting>>();
private Map correctAnswers = new HashMap<String, String>();

public void addQuestion(String question, ArrayList<String> answers, String correctAnswer)
{
   this.answers.put(question, answers);
   this.correctAnswers.put(question, correctAnswer);
}

[...]

When creating the answers map as above, your map would retain the array of questions, an array of arrays of answers and the association between each question and its answers array.

于 2013-02-06T12:46:28.733 回答
0

有许多可能的方法。其中之一是ArrayList为每个答案选项(即 A、B 和 C)创建一个选项。然后再创建一个选项ArrayList,表明哪个答案是正确的

因此总共将有 4ArrayList秒,其中 3 秒用于存储答案,1 秒用于指示哪个选项是正确的

于 2013-02-06T12:36:21.630 回答