我正在开发一个问题游戏的应用程序。我每次都设法将每个问题的答案显示到不同的按钮,但我无法检查正确的答案。我的方法是:我在数据库中创建了一个额外的列,我指出正确答案是(1、2、3 或 4)的列。我使用此代码在不同按钮中显示答案。
cur = dbHelper.getRandomQuestion();
String corrans = cur.getString(cur.getColumnIndex("CorrectAnswer"));
a = Integer.parseInt(corrans);
String question = cur.getString(cur.getColumnIndex("QUESTIONS"));
String answer0 = cur.getString(cur.getColumnIndex("ANSWER1"));
String answer1 = cur.getString(cur.getColumnIndex("ANSWER2"));
String answer2 = cur.getString(cur.getColumnIndex("ANSWER3"));
String answer3 = cur.getString(cur.getColumnIndex("ANSWER4"));
txtQuest.setText(question);
ArrayList<String> lstAnswers = new ArrayList<String>();
lstAnswers.add(answer0);
lstAnswers.add(answer1);
lstAnswers.add(answer2);
lstAnswers.add(answer3);
score.setText("Your score is " + b +","+ a);
Random random = new Random();
int[] textViews = new int[] { R.id.button1, R.id.button2, R.id.button3, R.id.button4 };
int textViewIndex = 0;
while (lstAnswers.size() > 0) {
int index = random.nextInt(lstAnswers.size());
if(a == index){ b = index;}
else{}
String randomAnswer = lstAnswers.remove(index);
((TextView)findViewById(textViews[textViewIndex])).setText(randomAnswer);
++textViewIndex;
}
对每个按钮调用以比较 a 和 b 的值,然后采取相应的行动。但它似乎不起作用。我明白为什么,但我无法弄清楚。有任何帮助。