java 从arraylist生成随机单词并检查它们是否在二维阵列棋盘游戏中

kyks70gy  于 2022-11-27  发布在  Java
关注(0)|答案(1)|浏览(80)

我需要从数组列表中生成12个随机单词,并将这个单词添加到2d数组中,2d数组是游戏的棋盘,而这个棋盘在另一个类WordSearch中。
这是我的代码,直到现在,请一些帮助,我不能理解如何使这个。

public static void readWords(){
    WordSearch search = new WordSearch();
    List<Word> poolWords = new ArrayList<Word>();

    String[] wordsArray = {"play", "dream", "personal", "advice", "steal", "suspicious", "borrow", "image", "repeat", "enemy", 
                        "break", "selfish", "protester", "charity", "encounter", "discreetly", "effectively", "react", "respect", "depression",
                        "couch", "counsellor", "snatch", "judge", "appearance", "quiet", "ridiculous", "overjoyed", "antidote", "parademic",
                        "employment", "balance", "overwhelm", "relax", "flextime", "task", "daily", "realistic", "essential", "stressful",
                        "fixed", "key", "reward", "salary", "loan", "promotion", "value", "database", "schedule", "priority",};

                        
                        for(String word: wordsArray){
                            poolWords.add(new Word(word));
                        }
                        
                        Random rand = new Random();
                        Scanner input = new Scanner(System.in);

                        System.out.println("How many words? max 12: ");
                        int maxWords = input.nextInt();
                        if(maxWords > 12){
                            System.out.println("i can generate only 12 words");
                        }
                        
                        
                        for(int i=0; i<maxWords; i++){
                            int random = rand.nextInt(poolWords.size());
                            Word randomWord = poolWords.get(random);
                            }
                            
                        }
58wvjzkj

58wvjzkj1#

您已经非常接近了,尽管您还没有提供任何有关类WordSearch以及如何向其中添加Word示例的信息。
下面是如何挑选随机数量的单词,注意,每次挑选后,我都会把那个单词从“池”中删除,这样它就不能再被选中了。
还要注意,它会不断向用户询问单词数,直到该值介于1和12之间(包括1和12):

public static void readWords(){
    WordSearch search = new WordSearch();
    List<Word> poolWords = new ArrayList<Word>();

    String[] wordsArray = {
      "play", "dream", "personal", "advice", "steal",
      "suspicious", "borrow", "image", "repeat", "enemy", 
      "break", "selfish", "protester", "charity", "encounter",
      "discreetly", "effectively", "react", "respect", "depression",
      "couch", "counsellor", "snatch", "judge", "appearance",
      "quiet", "ridiculous", "overjoyed", "antidote", "parademic",
      "employment", "balance", "overwhelm", "relax", "flextime",
      "task", "daily", "realistic", "essential", "stressful",
      "fixed", "key", "reward", "salary", "loan", "promotion",
      "value", "database", "schedule", "priority"
    };
                    
    for(String word: wordsArray){
        poolWords.add(new Word(word));
    }
    
    Random rand = new Random();
    Scanner input = new Scanner(System.in);
    
    int maxWords;
    do {
      System.out.print("How many words? (max 12): ");  
      maxWords = input.nextInt();
    } while (maxWords<1 || maxWords>12);
        
    for(int i=0; i<maxWords; i++){
      int random = rand.nextInt(poolWords.size());
      Word randomWord = poolWords.get(random);
      poolWords.remove(random);
      
      // ... do something with "randomWord" ...
      // ... somehow adding it to "search" ...
      //search.add(randomWord); // <-- you haven't shown us how to use WordSearch!
    }   
  }

相关问题