【问题标题】:Accessing each value in array in a random order以随机顺序访问数组中的每个值
【发布时间】:2021-03-18 13:36:50
【问题描述】:

我正在做一个猜谜游戏,你应该猜 x 国家的首都,如果用户想再次猜,应该每次都提示用户有一个 15 个 CountryCards 的数组,我需要通读随机顺序,我也不能两次使用同一张卡。当我运行我当前的代码时,我完成了 14 次数组迭代,然后最后一次只是卡在 while 循环的最开始(在输入 1 到 yesNo 之后没有任何反应)这是相关代码,任何帮助表示赞赏:

while (CountryCard.instances < 30) {
    System.out.println("Would you like to guess the capital of a country?"
            + " Hit 1 for yes, or 2 for no (Case sensitive)");
    yesNo = input.nextInt();
    if (yesNo == 2) {
        return;
    }
    Random generator = new Random();
    int randomNumber = generator.nextInt(game.length);
    while (game[randomNumber].used == true && CountryCard.uses < 15) {
        randomNumber = generator.nextInt(game.length);
    }
    System.out.println("What is the Capital of "
            + game[randomNumber].getName() + "?");
    game[randomNumber].usedCard(1);
    guess = input.next();
    if (guess.equals(game[randomNumber].getCapital())) {
        System.out.println("Correct! :)");
    } else {
        System.out.println("Incorrect! :(");
    }
}//end of while
System.out.println("Thanks for playing :)");

【问题讨论】:

    标签: java arrays random


    【解决方案1】:

    如果它只工作 14 次,那么问题将处于循环状态。
    尝试将CountryCard.uses&lt;15 更改为CountryCard.uses&lt;=15

    还要检查 game.length 的值

    【讨论】:

    • 不幸的是,这并不能解决问题。还是一样的错误
    • @Jonathan Bateman 我可以看看这些代码的逻辑吗?游戏[随机数].usedCard(1);而while(game[randomNumber].used==true
    • 你确定它卡住了吗?在这种情况下,我认为正在发生的事情是因为如果生成的随机数指向一个使用过的国家,那么您会在 while 循环中不停地生成随机数。东西是15个,其中14个已经被使用了!这可能是实施不佳而不是代码不起作用的情况。如果您继续生成随机数,那么很有可能您将获得最后一个未使用的国家/地区。
    【解决方案2】:

    为什么不每次玩游戏时都使用Collections.shuffle 将您的 CountryCard 列表随机排列?

    类似:

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.List;
    import java.util.Scanner;
    
    class Game {
      private static final List<CountryCard> countryCards = new ArrayList<>(
        Arrays.asList(
            new CountryCard("Canada", "Ottawa"),
            new CountryCard("USA", "Washington, D.C."),
            new CountryCard("France", "Paris"),
            new CountryCard("Germany", "Berlin"),
            new CountryCard("Spain", "Madrid"),
            new CountryCard("Turkey", "Ankara"),
            new CountryCard("Sudan", "Khartoum"),
            new CountryCard("Brazil", "Brasilia")
            // TODO add more countries as needed
         ));
    
      private Scanner input = new Scanner(System.in);
    
      public void play() {
        // shuffle the country card list before playing
        Collections.shuffle(countryCards);
        for (CountryCard randomCountryCard: countryCards) {
            System.out.println("What is the capital of " + randomCountryCard.getName() + "?");
    
            String guess = input.next();
    
            if (randomCountryCard.getCapital().equalsIgnoreCase(guess)) {
                System.out.println("Correct!");
            } else {
                System.out.println("Incorrect!");
            }
    
            System.out.println("Answer: " + randomCountryCard.getCapital() + "\n");
        }
      }
    
      public static void main(String[] args) {
        Game game = new Game();
        game.play();
      }
    }
    
    class CountryCard {
        private String name;
        private String capital;
    
        CountryCard(String name, String capital) {
            this.name = name;
            this.capital = capital;
        }
    
        public String getName() {
            return name;
        }
    
        public String getCapital() {
            return capital;
        }
    }
    

    【讨论】:

      【解决方案3】:

      如果您的集合/数组只有 15 个元素,您可以使用 Collections.shuffle() 然后对其进行迭代。

      如果您不想更改初始数组,您可以生成带有索引的集合(从零到 array.length),然后对其进行洗牌。

      【讨论】:

        【解决方案4】:

        我之前在评论中提到,由于您在查找最后一个 1/15 数字时在 while 循环中随机生成数字,因此很难做到。您可以做的是实现开放寻址冲突处理。其中,您可以使用线性探测或双重哈希。

        线性探测: 如果随机生成的数是x,x已经被使用,而不是生成新的随机数,取x+1。如果使用 x+1,则取 x+2,以此类推。

        双重哈希: 如果不使用随机生成的数字 x,请照常继续,但如果使用它,请使用散列将变量常量值添加到 x。

        【讨论】:

          猜你喜欢
          • 2012-11-19
          • 2021-03-09
          • 2016-01-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-03-28
          • 1970-01-01
          • 2016-12-08
          相关资源
          最近更新 更多