我试图创建为Android随机选择题测验。 我想从一个字符串数组显示的随机质询,与来自另一个字符串数组表示的四个选项之一了相应的答案。 其他3个选择将来自另一个字符串数组,将用于提供所有问题的“错误”的答案,随机。
两个问题:是否有更好的方法,使一个选择题测验这样吗? - 和 - 当玩家选择一个答案,我怎么确定的答案来自哪个阵列?
这是我使用随机的代码:
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
我建议创建一个名为QuestionAndAnswer新类。 该类应持的问题,正确的答案,它也可以容纳任何自定义错误的答案和用户的选择。 确切的实现是完全由你决定。
在您的活动必须通过这个列表类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);
}
}
因为我改变了你的四个答案TextViews到RadioGroup中的活动,这样一来,用户可以直观地选择一个答案。 我还以为会有prev
和next
按钮,他们将调整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++;
这是我的总体思路。 该代码是一个完整的思想,所以它会编译。 当然,你需要添加“下一个”按钮来查看不同的问题。 但是,这是足以让你看到你的随机化的问题和答案,同时保持他们组织的一种方式。
这里的样本,你可以试试。 这是数据模型喜欢抱着对问答的事情的东西。
<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>
好了,你每次你显示一个问题,你把所有的选项来回答,并且每个问题的正确答案。 只要创建一个合适的数据结构来保存它们。 不管怎么说,只是一个样本,而不是一个完美的人,但给它一个尝试,如果你对这个新充塞^^!