我正在尝试学习如何构建一个简单的mcq图片测验应用程序。我有此代码用作模板。显示一张图片,用户必须选择4个选项/答案之一。问题是4个选项不是应有的4个不同答案。甚至似乎您玩的越多,答案就越少。有人可以帮助我了解问题是什么吗?
//picks a random number for the answer
correctAnswer = 0 + (int)(Math.random() * ((3 - 0) + 1));
//create an array of answers from file names
ArrayList<String> answers = new ArrayList<String>();
List<Integer> categoryList = new ArrayList<Integer>();
for (int subArrayFlag = 0; subArrayFlag < imagesShuffled.size();subArrayFlag++){
if(imagesShuffled.get(currentQuestion).getName().substring(0,1)
.equalsIgnoreCase(imagesShuffled.get(subArrayFlag).getName().substring(0,1))){
categoryList.add(subArrayFlag);
}
}
//get 3 random answers and add it to the array
for (int i = 0 ; i < 4 ;i++ ){
if (i == correctAnswer){
answers.add(imagesShuffled.get(currentQuestion).getName());
}else {
do {
randomFileIndex = (int) (Math.random() * ((categoryList.size() - 1) + 1));
} while ( categoryList.get(randomFileIndex) == currentQuestion && categoryList.size() > 0 );
answers.add(imagesShuffled.get(categoryList.get(randomFileIndex)).getName());
}
}
try
{
// get input stream
InputStream ims = getAssets().open(imagesShuffled.get(currentQuestion).getPath());
// load image as Drawable
Drawable d = Drawable.createFromStream(ims, null);
// set image to ImageView
mainIV.setImageDrawable(d);
}
catch(IOException ex)
{
return;
}
btn1.setText(answers.get(0).substring(2));
btn2.setText(answers.get(1).substring(2));
btn3.setText(answers.get(2).substring(2));
btn4.setText(answers.get(3).substring(2));
btn1.setBackgroundDrawable(d);
btn2.setBackgroundDrawable(d);
btn3.setBackgroundDrawable(d);
btn4.setBackgroundDrawable(d);
}