简体   繁体   English

Java:具有随机选择顺序的猜谜游戏

[英]Java: Guessing game with randomized choice order

I'm trying to create a country quiz guessing game however, I have run into a problem. 我正在尝试创建国家/地区猜谜游戏,但是遇到了问题。 I want there to be a menu where a country is displayed and underneath there should be three capitals to choose from of which one is correct. 我希望有一个显示国家的菜单,下面应该有三个大写字母可供选择,其中一个是正确的。 The problem is I don't want the correct answer to be at the same position for every country, in other words I want the order of the alternatives to be randomized but, I'm not sure of how this could be done... 问题是我不希望每个国家的正确答案都在同一位置,换句话说,我希望将备选方案的顺序随机化,但是我不确定如何做到这一点...

I have not come far with my code but here is my method: 我的代码还不完善,但这是我的方法:

  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);
  }


}

I want the position of list[i].stad to be randomized 我想将list [i] .stad的位置随机化

This is my LandStad class: 这是我的LandStad课程:

public class LandStad {

    String land;
    String stad;
}

Why not just shuffle the array that you get? 为什么不只是随机排列获得的数组? That way you can keep the JOptionPane dialogue the same, ie: 这样,您就可以保持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);
}

You can use something like: 您可以使用类似:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM