[英]Java: Guessing game with randomized choice order
我正在尝试创建国家/地区猜谜游戏,但是遇到了问题。 我希望有一个显示国家的菜单,下面应该有三个大写字母可供选择,其中一个是正确的。 问题是我不希望每个国家的正确答案都在同一位置,换句话说,我希望将备选方案的顺序随机化,但是我不确定如何做到这一点...
我的代码还不完善,但这是我的方法:
public static void gissaStadAlt(LandStad[] list) {
for(int i = 0; i < list.length; i++) {
int rand = (int)(Math.random() * 10);
JOptionPane.showInputDialog(null, "Which of the alternatives is the capital in " + list[i].land + "?" + "\n" +
"1." + list[rand].stad + "\n" +
"2." + list[i].stad + "\n" +
"3." + list[rand].stad);
}
}
我想将list [i] .stad的位置随机化
这是我的LandStad课程:
public class LandStad {
String land;
String stad;
}
为什么不只是随机排列获得的数组? 这样,您就可以保持JOptionPane对话不变,即:
public static void gissaStadAlt(LandStad[] list)
{
Collections.shuffle(Arrays.asList(list));
JOptionPane.showInputDialog(null, "Which of the alternatives is the
capital in " + list.getLand() + "?" + "\n" +
"1." + list[0].city + "\n" +
"2." + list[1].city + "\n" +
"3." + list[2].city);
}
您可以使用类似:
public static void gissaStadAlt(LandStad[] list) {
for(int i = 0; i < list.length; i++) {
String[] options = new String[3];
int rand = i;
while(rand!=i){
rand = (int)(Math.random() * 10);
}
options[0] = list[rand].city;
int k = rand;
while(rand!=i && rand!=k){
rand = (int)(Math.random() * 10);
}
options[1] = list[rand].city;
options[2] = list[i].city;
int[] randomize = {123,132,213,231,312,321};
int num = randomize[(int)(Math.random() * 6)];
for(int i=0;i<3;i++){
int g = num/100;
options[i] = g + ". " + options[i];
num%=100;
num*=10;
}
Arrays.sort(options);
JOptionPane.showInputDialog(null, "Which of the alternatives is the capital in " + list[i].land + "?" + "\n" +
options[0] + "\n" +
options[1] + "\n" +
options[2]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.