簡単なクイズゲームのやり方を学んでいます。ゲームでは、クリック後に新しい質問が表示されるように乱数が生成されます (各ケースが 1 つの異なる質問に関連付けられているスイッチがあります)。各質問には 1 つの正解しかなく、いずれかのボタンをクリックすると次の質問が表示されます (ユーザーが右ボタンをクリックしたかどうかは関係ありません)。しかし、私はボタンもランダムに表示しようとしています...私はこれを試しました:
final Random rng = new Random();
final List<Integer> generated = new ArrayList<Integer>();
while(true){
if(generated.size()!=3) {
Integer nxt = rng.nextInt(3) + 1;
if (!generated.contains(nxt))
{
generated.add(nxt);
textView1 = (TextView) findViewById(R.id.textView1);
switch (nxt) {
case 1:
textView1.setText("Question 1");
btn1.setText("Right answer");
btn2.setText("Wrong 1");
btn3.setText("Wrong 2");
btn4.setText("Wrong 3");
break;
case 2:
textView1.setText("Question 2");
btn1.setText("Right answer");
btn2.setText("Wrong 1");
btn3.setText("Wrong 2");
btn4.setText("Wrong 3");
break;
case 3:
textView1.setText("Question 3");
btn1.setText("Right answer");
btn2.setText("Wrong 1");
btn3.setText("Wrong 2");
btn4.setText("Wrong 3");
break;
}
break;
}
}
}
このコードを onCreate (ユーザーがこのアクティビティの質問を入力したときに既に表示されている質問) とボタン 1 の onClick (ボタンをクリックすると別の質問が表示される) に入れました。 ) ランダム化する必要があります。そのようなことをする最善の方法は何ですか?