为什么我的问题和照片没有相同的结果

时间:2018-09-06 15:17:08

标签: java android

我正在尝试制作一个Android问答游戏,该游戏将我的问题和图像随机化,以帮助回答问题..但在游戏中尝试2次后,随机值不会返回相同的结果。randomNum是我用于获取随机数..我希望两个问题都可以同步..

Random random = new Random();
//get random number between 0 to 9.
int randomNum = random.nextInt(quizArray.size());

//image
imageView.setBackgroundResource(image[randomNum]);
//pick quiz
ArrayList<String> quiz = quizArray.get(randomNum);

//set question and answer
questionLabel.setText(quiz.get(0));
rightAnswer = quiz.get(1);

//remove question

quiz.remove(0);
Collections.shuffle(quiz);

//set Choices
btn1.setText(quiz.get(0));
btn2.setText(quiz.get(1));
btn3.setText(quiz.get(2));
btn4.setText(quiz.get(3));

//remove quiz from quizArray
quizArray.remove(randomNum);
}

3 个答案:

答案 0 :(得分:1)

当您使用此quizArrayquizArray.remove(randomNum);删除测验时,您还需要从image[]表中删除相关图片,我建议您使用List图片而不是table,因此您也可以在image.remove(randomNum);之后使用quizArray.remove(randomNum);

我希望这对您有帮助

答案 1 :(得分:0)

正如BM所说,您似乎正在从测验阵列中删除测验,但并未从图像阵列中删除图像-这导致它们的索引关闭。

也许您想尝试使用HashMap(测验,图像),然后使用随机索引从地图中获取一对。

答案 2 :(得分:0)

为什么不使用QuestionquestionStringanswerString的字段创建image类。然后,您可以创建Question对象的列表,只需从列表中删除Question即可删除所有关联的问题数据。

class Question {
    public final String questionString;
    public final String answerString;
    public final byte[] image;

    public Question(String question, String answer, byte[] image) {
        questionString = question;
        answerString = answer;
        this.image = image;
    }
}
...
List<Question> questions = new ArrayList<>();
//Populate questions however
//I would suggest reading the questions from an XML file
//Instead of static array declarations for ease of use in the future

System.out.println(questions);
questions.remove(0);
System.out.println(questions);