我正在制作一个“谁想要成为百万富翁”的大学作业测验游戏类型,但我找不到一种方法可以将我的字符串数组中的4个答案字符串随机分配到我的4个答案JButtons而不重复。我尝试了一些我在网上找到的解决方案,我设法做的最好的是随机设置文本,但有机会复制(当然只有4个字符串,因此相当高的机会)。
有人可以帮忙吗?
if (currentGKQNum <=9){
currentQuestion = Game.getGKQuestion(currentGKQNum); //gets a question object at the index specified by currentGKQNum
questionLabel.setText(currentQuestion.getQuestion()); //gets the actual question string from the question object and assigns it to question label
currentGKQNum += 1;
}
else{
questionLabel.setText("End of general knowledge questions");
}
String[] answers = new String[] {currentQuestion.getAnswer1(),currentQuestion.getAnswer2(),currentQuestion.getAnswer3(),currentQuestion.getCorrectAnswer()};
answer1Button.setText(//need random string from answers[] here)
answer2Button.setText(//need random string from answers[] here)
answer3Button.setText(//need random string from answers[] here)
answer4Button.setText(//need random string from answers[] here)
答案 0 :(得分:1)
将数组转换为列表并随机播放列表。
value
然后从列表中获取文本。