块引用
对不起,我想问我有一个想要随机测验使用数组的情况但不能随机化每个问题,只有一个问题被改变,也许可以有帮助和解决方案的朋友 在这里输入代码
private int pertanyaan [] = {R.raw.correct,R.raw.wrong,
R.raw.correct,R.raw.wrong,R.raw.correct,R.raw.wrong,R.raw.correct,R.raw.wrong,R.raw.assalamu_alaikum,R.raw.correct}; < / p>
public String pilihanganda [] ={
"Perhatikan gambar tersebut gambar ?, carilah seperti gambar diatas",
"Perhatikan gambar tersebut gambar ?, carilah seperti gambar diatas",
"Perhatikan gambar tersebut gambar ?, carilah seperti gambar diatas",
"Perhatikan gambar tersebut gambar ?, carilah seperti gambar diatas",
"Perhatikan gambar tersebut gambar ?, carilah seperti gambar diatas",
"Perhatikan gambar tersebut gambar ?, carilah seperti gambar diatas",
"Perhatikan gambar tersebut gambar ?, carilah seperti gambar diatas",
"Perhatikan gambar tersebut gambar ?, carilah seperti gambar diatas",
"Perhatikan gambar tersebut gambar ?, carilah seperti gambar diatas",
"Perhatikan gambar tersebut gambar ?, carilah seperti gambar diatas",
};
private int pilihan_a [] ={R.drawable.hidung, R.drawable.wajah, R.drawable.rambut, R.drawable.pergelangan_kaki,
R.drawable.telinga, R.drawable.rambut, R.drawable.leher, R.drawable.lengan, R.drawable.rambut
, R.drawable.telinga};
private int pilihan_b[] ={R.drawable.tangan, R.drawable.rambut,R.drawable.leher, R.drawable.tangan,
R.drawable.pergelangan_kaki, R.drawable.leher, R.drawable.rambut, R.drawable.lengan, R.drawable.telinga
, R.drawable.pundak};
private int pilihan_c[] ={R.drawable.pundak, R.drawable.lengan, R.drawable.tangan, R.drawable.pundak, R.drawable.pergelangan_kaki
, R.drawable.tangan, R.drawable.pergelangan_kaki, R.drawable.wajah, R.drawable.lengan,
R.drawable.rambut};
private int pilihan_d[] ={R.drawable.pergelangan_kaki, R.drawable.tangan, R.drawable.hidung, R.drawable.pundak,
R.drawable.rambut, R.drawable.pundak, R.drawable.tangan, R.drawable.leher, R.drawable.telinga, R.drawable.pergelangan_kaki};
public static String [] jawaban ={"A","B","B","C","A","C","D","C","C","B"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quiz_2);
player = MediaPlayer.create(this, pertanyaan[indeks]);
soal = (Button)this.findViewById(R.id.button2);
soal.setBackgroundResource(pertanyaan[indeks]);
soal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.v(TAG,"Playing Sound...");
player.start();
}
});
soalnya = (TextView)findViewById(R.id.Soal);
soalnya.setText(pilihanganda[0]);
player = MediaPlayer.create(this, pertanyaan[indeks]);
imgB=(ImageButton)findViewById(R.id.pilihan2);
imgC=(ImageButton)findViewById(R.id.pilihan3);
imgD=(ImageButton)findViewById(R.id.pilihan4);
imgA=(ImageButton)findViewById(R.id.pilihan1);
imgA.setImageResource(pilihan_a[indeks]);
imgA.setOnClickListener(this);
imgB.setImageResource(pilihan_b[indeks]);
imgB.setOnClickListener(this);
imgC.setImageResource(pilihan_c[indeks]);
imgC.setOnClickListener(this);
imgD.setImageResource(pilihan_d[indeks]);
imgD.setOnClickListener(this);
nomor = (TextView) findViewById(R.id.No);
nomor.setText("Soal " + no++);
salahbenar =(TextView)findViewById(R.id.salahbenar);
salahbenar.setText("");
}
答案 0 :(得分:0)
如果你有一个数组问题[]或答案[],并且你需要x个问题,例如它可能是10,你可以按原样创建一个问题列表:
Random rand = new Random();
int questionLength = 10; //the number questions you will use
int[] myList = new int[questionLength];
for (int i = 0; i < questionLength; i++) {
myList[i] = rand.nextInt( allQuestionlist.size() );
}
此数组将成为随机问题的索引,例如:
for (int i = 0; i < questionLength; i++) {
System.out.println( pilihanganda[myList[i]] ); // printing
}
答案 1 :(得分:0)
如果您要使用String数组中的某些值,那么您可以使用以下答案,但如果您要使用所有问题,请尝试以下操作:
以相同的方式洗牌两个阵列:
private voice Integer [] question = {R.raw.bahu, R.raw.dagu, R.raw.rambut, R.raw.kaki, R.raw.mata, R.raw.telinga, R.raw.gigi, R.raw.wajah, R.raw.lidah, R.raw.hidung};
让我们说下面的数组就是你的答案数组
private int pilihan_a [] ={R.drawable.hidung, R.drawable.wajah, R.drawable.rambut, R.drawable.pergelangan_kaki,
R.drawable.telinga, R.drawable.rambut, R.drawable.leher, R.drawable.lengan, R.drawable.rambut
, R.drawable.telinga};
然后使用它以相同的方式对这两个数组进行混洗:
long seed = System.nanoTime();
List<String> questionList = Arrays.asList(question);
List<String> answerList = Arrays.asList(pilihan_a);
Collections.shuffle(questionList, new Random(seed));
Collections.shuffle(answerList, new Random(seed));
//Now both question and answer array has randomly shuffled elements in it in the same way
pilihan_a = answerList.toArray(new String[myList.size()]);
question = questionList.toArray(new String[myList.size()]);
希望它能解决你的问题。