[英]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.