私は 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) のような他のインデックスと比較しようとすると、 比較のために選択された「スロット」に最初の質問テキストの回答がまだ残っているために発生すると考えられます。それで、私は何をすべきですか?
*さらに: コードが非常に大きくなっていますif
。onClickListener にを入れid
て 4 つのボタンと比較しようとしましたが、onCreate 以外の他の関数を使用する必要がありました。 objects) が宣言されておらず、別の ArrayList を宣言する必要があるため、クリックされたボタンを比較できません。私がやりたいことをするためのより良い方法はありますか?