我正在开发一个智力竞赛应用程序,除了一件主要的事情——打乱答案之外,我还有一个工作逻辑。这个测试应该是可以回放的,所以每次用户播放时我都需要打乱答案,这样他们就不会记得顺序和正确答案了。它现在是如何工作的:我有一个类,其中所有内容都已定义。我也有其他基于难度的课程(中等、难(,但现在已经不相关了。
课堂问题:
public class Question {
//Questions
public String easyQuestions[] = {
App.getAppResources().getString(R.string.eqwf1),
App.getAppResources().getString(R.string.eqwf2),
...
private String easyChoices[][] = {
{App.getAppResources().getString(R.string.eqwf1_a1),App.getAppResources().getString(R.string.eqwf1_a2),App.getAppResources().getString(R.string.eqwf1_a3),App.getAppResources().getString(R.string.eqwf1_a4)},
{App.getAppResources().getString(R.string.eqwf2_a1),App.getAppResources().getString(R.string.eqwf2_a2),App.getAppResources().getString(R.string.eqwf2_a3),App.getAppResources().getString(R.string.eqwf2_a4)},
...
//Correct answers
private String easyCorrectAnswers [] = {
App.getAppResources().getString(R.string.eqwf1_ac),
App.getAppResources().getString(R.string.eqwf2_ac),
...
//Get question
public String getEasyQuestion(int que){
String question = easyQuestions[que];
return question;
}
//Get first answer
public String getAnswer1(int a){
String choice = easyChoices[a][0];
return choice;
}
//Get second answer
public String getAnswer2(int a){
String choice = easyChoices[a][1];
return choice;
}
//Get third answer
public String getAnswer3(int a){
String choice = easyChoices[a][2];
return choice;
}
//Get fourth answer
public String getAnswer4(int a){
String choice = easyChoices[a][3];
return choice;
}
//Get correct answer
public String getCorrectAnswer(int a){
String answer = easyCorrectAnswers[a];
return answer;
}
在我的活动中,我有以下功能,可以处理洗牌问题和更新:
主要活动:
private void generateQuestions(List<Integer> rQList, int lengthOfWholeArray){
Random rand = new Random();
while (rQList.size() != lengthOfWholeArray) {
int r = rand.nextInt(lengthOfWholeArray);
if (!rQList.contains(r))
rQList.add(r);
}
}
private void generateQuestionsBasedOnDifficulty(List<Integer> list, int difficulty){
switch (difficulty){
case 1:
generateQuestions(list, easyQuestionsLength);
break;
case 2:
generateQuestions(list, mediumQuestionsLength);
break;
case 3:
generateQuestions(list, hardQuestionsLength);
break;
}
}
private void updateEasyQuestions(int num) {
question.setText(easyQuestions.getEasyQuestion(num));
answer1.setText(easyQuestions.getAnswer1(num));
answer2.setText(easyQuestions.getAnswer2(num));
answer3.setText(easyQuestions.getAnswer3(num));
answer4.setText(easyQuestions.getAnswer4(num));
mAnswer = easyQuestions.getCorrectAnswer(num);
}
我认为生成困难的函数在这里并不重要,但我把它们放在这里,这样你就知道它是如何工作的了。然后,按下"下一步"按钮后,问题更新:
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (index == questionsLength) {
gameOver();
} else {
new Timer().schedule(new TimerTask() {
@Override
public void run() {
switch (difficulty) {
case 1:
updateEasyQuestions(questionList.get(index));
index++;
break;
case 2:
updateMediumQuestions(questionList.get(index));
index++;
break;
case 3:
updateHardQuestions(questionList.get(index));
index++;
break;
}
}
},700);
refreshUI();
}
}
});
计时器在这里是因为动画,最相关的是函数的调用。我需要以某种方式重写updateEasyQuestions,这样每个问题的答案都会被打乱。或者创建新功能。
答案1,回答2,。。。是按钮。refreshUI只是一个处理图形、动画和UI的简单功能(例如,当你回答时无法点击按钮(。
希望它有意义。现在有这么多代码,但正如我所说,最重要的部分是类本身和更新问题的函数。每一个想法(以及改进我的代码的建议(都受到欢迎!谢谢
我不确定我是否正确理解你的问题,但我会创建一个question类,该类将有一个Answer对象列表,如果这是正确的答案,每个对象都有一个布尔值和一个带有随机数的整数,在演示过程中,它们将按整数排序,也使用ThreadLocalRandom进行随机化