好吧,我是 Java 编程的新手,按照我在这里看到的一些技巧,我将这段代码制作成了一个问答游戏:
public class OtherActivity extends Activity {
TextView textView1, textView2;
Button btn1, btn2, btn3, btn4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
textView1 = (TextView) findViewById(R.id.textView1);
textView2 = (TextView) findViewById(R.id.textView2);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
btn4 = (Button) findViewById(R.id.btn4);
final ArrayList<Question> qsts = new ArrayList<Question>();
qsts.add(Question.q1);
qsts.add(Question.q2);
qsts.add(Question.q3);
qsts.add(Question.q4);
qsts.add(Question.q5);
qsts.add(Question.q6);
final Random rng = new Random();
final List<Integer> generated = new ArrayList<Integer>();
int nxt = rng.nextInt(6);
generated.add(nxt);
final Question nextQuestion = qsts.get(nxt);
textView1.setText(nextQuestion.questionText);
final ArrayList<String> allAnswers = new ArrayList<String>();
allAnswers.add(nextQuestion.correctAnswerText);
allAnswers.add(nextQuestion.wrongAnswer1);
allAnswers.add(nextQuestion.wrongAnswer2);
allAnswers.add(nextQuestion.wrongAnswer3);
Collections.shuffle(allAnswers);
btn1.setText(allAnswers.get(0));
btn2.setText(allAnswers.get(1));
btn3.setText(allAnswers.get(2));
btn4.setText(allAnswers.get(3));
btn1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(allAnswers.get(0) == nextQuestion.correctAnswerText){
textView2.setText("CORRECT ANSWER, MAN!");
while(true){
int nxt = rng.nextInt(6);
if (!generated.contains(nxt)){
generated.add(nxt);
Question nextQuestion = qsts.get(nxt);
textView1.setText(nextQuestion.questionText);
ArrayList<String> allAnswers = new ArrayList<String>();
allAnswers.add(nextQuestion.correctAnswerText);
allAnswers.add(nextQuestion.wrongAnswer1);
allAnswers.add(nextQuestion.wrongAnswer2);
allAnswers.add(nextQuestion.wrongAnswer3);
Collections.shuffle(allAnswers);
btn1.setText(allAnswers.get(0));
btn2.setText(allAnswers.get(1));
btn3.setText(allAnswers.get(2));
btn4.setText(allAnswers.get(3));
break;
}
}
}
else{
textView2.setText("WRONG ANSWER, MAN!");
while(true){
int nxt = rng.nextInt(6);
if (!generated.contains(nxt)){
generated.add(nxt);
// ----> Integer nxt = rng.nextInt(6); <---- random nxt aqui!
Question nextQuestion = qsts.get(nxt);
textView1.setText(nextQuestion.questionText);
ArrayList<String> allAnswers = new ArrayList<String>();
allAnswers.add(nextQuestion.correctAnswerText);
allAnswers.add(nextQuestion.wrongAnswer1);
allAnswers.add(nextQuestion.wrongAnswer2);
allAnswers.add(nextQuestion.wrongAnswer3);
Collections.shuffle(allAnswers);
btn1.setText(allAnswers.get(0));
btn2.setText(allAnswers.get(1));
btn3.setText(allAnswers.get(2));
btn4.setText(allAnswers.get(3));
break;
}
}
}
}
});
//
// AND THE SAME METHOD TO THE BUTTONS btn2, btn3, btn4
//
// btn2 with a if (allAnswers1.get(1) == nextQuestion.correctAnswerText) { ...
// btn3 with a if (allAnswers1.get(2) == nextQuestion.correctAnswerText) { ...
// btn4 with a if (allAnswers1.get(3) == nextQuestion.correctAnswerText) { ...
//
}
}
我有这个其他类代码:
public class Question {
String questionText;
String correctAnswerText;
String wrongAnswer1;
String wrongAnswer2;
String wrongAnswer3;
Question (String qst, String cAns, String wAns1, String wAns2, String wAns3){
questionText = qst;
correctAnswerText = cAns;
wrongAnswer1 = wAns1;
wrongAnswer2 = wAns2;
wrongAnswer3 = wAns3;
}
static Question q1 = new Question(
"Question 1",
"Correct answer - question 1",
"Wrong Answer 1 - question 1",
"Wrong Answer 2 - question 1",
"Wrong Answer 3 - question 1"
);
static Question q2 = new Question(
"Question 2",
"Correct answer - question 2",
"Wrong Answer 1 - question 2",
"Wrong Answer 2 - question 2",
"Wrong Answer 3 - question 2"
);
// ...
// and the same with q3, q4, q5 and q6
// ...
好吧,你怎么看,我在识别正确答案时遇到了问题。在第一个问题中它工作正常,但在此之后就不行了。我相信这个问题会发生,因为当我尝试比较 allAnswers.get(0) == nextQuestion.correctAnswerText
或与 ArrayList allAnswers 的 (1)、(2) 或 (3) 等其他索引进行比较时,为比较选择的“槽”仍然具有第一个问题文本答案。所以我该怎么做?
*Plus:代码变得非常大,我尝试if
在 onClickListener 中放入一个并与id
4 个按钮中的一个进行比较,但它必须与 onCreate 之外的其他函数一起使用,因为 ArrayList qsts(包含问题objects) 未声明,我无法比较单击的按钮,因为我必须声明一个不同的 ArrayList。有更好的方法来做我想做的事吗?