Java 如何从字符串中获取随机单词?

Java 如何从字符串中获取随机单词?,java,Java,我想用Java创建一个刽子手游戏,但我不确定如何设置整个游戏。我正在研究一个系统,他们的字母越多,出现在绞刑架上的几率就越小 import java.ultil.Scanner; public class Hangman { public static void main(string[]args); // Let's try a main? scr = newScanner(system.in); // used for keyboard input

我想用Java创建一个刽子手游戏,但我不确定如何设置整个游戏。我正在研究一个系统,他们的字母越多,出现在绞刑架上的几率就越小

    import java.ultil.Scanner;
    public class Hangman {
      public static void main(string[]args); // Let's try a main?
      scr = newScanner(system.in); // used for keyboard input
      string words = "Hi:Bye:Hello:Cheese:Puppies";
      int length = words.length();
      }
    }
如何从变量“words”中获取一个随机单词,并计算其长度

请记住,我并不擅长编写Java代码

String words = "Hi:Bye:Hello:Cheese:Puppies";

String[] wordsSplit = words.split(":");
然后将结果数组随机化。但正如其他人所指出的,你也可以从一系列单词开始

请参阅文档:


你有语法错误。获取一个IDE。

如果您希望使用计数最高的单词,那么您的问题不应该是关于随机单词的。它应该是关于按长度排序单词,然后从前N个最长单词中随机挑选一个单词

创建一个数组并添加单词

    StringTokenizer tokenizer = new StringTokenizer("Boy:Chicken:mango", ":");

    String [] words = new String [tokenizer.countTokens()];
            int counter =0;
            while (tokenizer.hasMoreElements()) {
                String word = (String) tokenizer.nextElement();
                words[counter] = word;
                counter++;
            }
如果要选择计数最高的单词,请按最高计数对此处的单词进行排序

您可以在hashmap中放置,然后迭代选择具有最高

HashMap<String, Integer> count = new HashMap<String, Integer>();

    for (String word : words) {
        if (!count.containsKey(word)) {
            count.put(word, word.length());
        }
    }

为了实现真正有效的排序,您需要为单词编写自己的比较器(基于长度,但首先是较长的单词),然后创建一个优先级队列,将单词添加到其中,当您执行remove()时,您将得到长度最高的单词

这一部分的答案是随机选取一个单词并找出其长度

String words = "Hi:Bye:Hello:Cheese:Puppies";
String[] wordsAsArray = words.split(":");

int index = new Random().nextInt(wordsAsArray.length);

String randomWord = wordsAsArray[index];
System.out.println("Random word: '" + randomWord + "'. It is of length: " + randomWord.length());

要获取任何给定
字符串的长度,只需使用:

string.length();
为了获得一个随机单词(每个单词根据其长度出现的几率不同),首先需要将单词添加到某种列表中,如下所示:

ArrayList<String> words = new ArrayList<String>();
words.add("word");
words.add("word2");
words.add("etc");
selectRandomWord(words);

以下是一些适用于您尝试执行的操作的代码:

public class Hangman {

    public static void main(String[] args) {
        Scanner scr = new Scanner(System.in); //Used for keyboard input
        List<String> words = new ArrayList<String>(); {
            words.add("Hi");
            words.add("Bye");
            words.add("Hello");
            words.add("Cheese");
            words.add("Puppies");
        }

        Random numberGenerator = new Random(); //Creates Random object
        int randomNum = numberGenerator.nextInt(words.size()); //Return a num between 0 and words.size()-1

        System.out.println(randomNum); //Prints the random number outputted by the generator
        System.out.println(words.get(randomNum)); //Retrieves the String located at the index value 
        System.out.println(words.get(randomNum).length()); //Returns the size of the words
    }
}
公共级刽子手{
公共静态void main(字符串[]args){
Scanner scr=新扫描仪(System.in);//用于键盘输入
List words=new ArrayList(){
字。加上(“Hi”);
字。加上(“再见”);
添加(“你好”);
词语。添加(“奶酪”);
词语。添加(“小狗”);
}
Random numberGenerator=new Random();//创建随机对象
int randomNum=numberGenerator.nextInt(words.size());//返回介于0和words.size()之间的数值-1
System.out.println(randomNum);//打印生成器输出的随机数
System.out.println(words.get(randomNum));//检索位于索引值处的字符串
System.out.println(words.get(randomNum.length());//返回单词的大小
}
}

将单词放在一个列表中,然后从随机索引中选择一个。您的代码段中充满了语法错误。@Jivings我被与void(string[]args)混淆了。公平地说,Java技能水平反映在问题中发布的代码中的人除了“我的第一个Java程序”教程之外,还没有准备好处理任何其他问题。Haque1,这就是我现在正在做的事。:)谢谢你的建议。:)同样的,每个人都有相同的几率word@MrD那么你有什么建议?@Gamb回答提问者问题的方法,我正在研究on@MrD这就是我想听到的(读…),我更新了答案。创建一个包含单词及其长度的hashmap。然后,您可以对其进行迭代,并选择长度最大的单词。要以更有效的方式对单词进行排序,您需要编写自己的比较器,并使用优先级队列之类的东西放入他的问题是:“如何从变量“words”中获得一个随机单词,并计算其长度?”我的答案是他得到了单词及其长度——剩下的问题由他自己来解决。我的计划是将单词分成困难组,如难、中、易。但我不完全确定我将从那里走到哪里。你完全正确,我在没有Eclipse的情况下在课堂上编写了该脚本,而且由于我对该语言非常陌生,我知道我真的需要一个IDE。
selectRandomWord(words);
public class Hangman {

    public static void main(String[] args) {
        Scanner scr = new Scanner(System.in); //Used for keyboard input
        List<String> words = new ArrayList<String>(); {
            words.add("Hi");
            words.add("Bye");
            words.add("Hello");
            words.add("Cheese");
            words.add("Puppies");
        }

        Random numberGenerator = new Random(); //Creates Random object
        int randomNum = numberGenerator.nextInt(words.size()); //Return a num between 0 and words.size()-1

        System.out.println(randomNum); //Prints the random number outputted by the generator
        System.out.println(words.get(randomNum)); //Retrieves the String located at the index value 
        System.out.println(words.get(randomNum).length()); //Returns the size of the words
    }
}