【问题标题】:Java: Guessing game with randomized choice orderJava:随机选择顺序的猜谜游戏
【发布时间】:2015-11-14 18:59:07
【问题描述】:

我正在尝试创建一个国家测验猜谜游戏,但是我遇到了一个问题。我希望有一个显示国家/地区的菜单,下面应该有三个大写字母可供选择,其中一个是正确的。问题是我不希望每个国家/地区的正确答案都处于相同的位置,换句话说,我希望备选方案的顺序是随机的,但是,我不确定如何做到这一点......

我的代码还没有走多远,但这是我的方法:

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

【问题讨论】:

  • 请添加您拥有的代码。你也不能只使用随机数生成器
  • 如果您使用列表来保存您的备选方案,您可以简单地调用列表上的 shuffle 方法。
  • 您的代码不完整。 LandStad 是什么?所需的进口在哪里?你的班级声明在哪里?见How to create a Minimal, Complete, and Verifiable example。
  • @Jubobs:我认为——作为母语人士——land stad 是 CountryCity 的荷兰语。这是一个猜测国家首都的测验。
  • @CommuSoft 谢谢,但我并没有问这个词本身的含义。为了完整起见,我只想查看类定义。

标签: java random country


【解决方案1】:

为什么不直接打乱你得到的数组呢?这样您就可以保持 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);
}

【讨论】:

    【解决方案2】:

    你可以使用类似的东西:

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

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-29
      • 2018-09-11
      • 2019-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多