我需要一种方法来组合3个可能的答案,在一个ArrayList的问题,其中只有一个答案是正确的。
我是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;
}
}发布于 2013-02-06 20:36:22
有许多可能的方法。其中之一是为每个答案选项创建一个ArrayList,即A、B和C。然后再创建一个指示哪个答案正确的ArrayList
因此,总共有4个ArrayList,3个用于存储答案,1个用于指示哪个选项是正确的
发布于 2013-02-06 20:46:29
当必须将数据与其他数据相关联时,映射非常有用。在您的示例中,您正在尝试将问题与每个问题的答案数组相关联。
您可以创建一个HashMap<String, ArrayList<String>> answers并保留所有问题的答案( ArrayList<String>) ( String),然后创建另一个HashMap<String, String> correctAnswers,在其中保留问题(第一个String)及其相关的正确答案(第二个String)。
在验证答案时,您必须获得用户选择的答案,并将其与正确答案进行比较,如下所示:
if(correctAnswers.get(questionString).compareTo(chosenAnswerString))
{
//correct answer!
}
else
{
//incorrect answer
}用来添加问题及其答案的方法,您将变成:
[...]
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);
}
[...]在如上创建answers映射时,您的映射将保留问题数组、答案数组以及每个问题与其答案数组之间的关联。
https://stackoverflow.com/questions/14729148
复制相似问题