2013-05-14 78 views
-3

我想用Java创建一个hang子手游戏,但我不确定如何设置整个事情。我正在研究一个系统,他们有更多的信件,他们有更少的机会出现在hang子手之谜上。如何从字符串中获取随机单词?

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的最佳人选。

+4

把话说在一个列表中,选择一个在一个随机指数。 – 2013-05-14 18:08:41

+9

您的代码片段充满了语法错误。 – Jivings 2013-05-14 18:09:04

+0

@Jivings我与虚空混淆(string [] args) – 2013-05-14 18:09:41

回答

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

String[] wordsSplit = words.split(":"); 

然后随机化生成的数组。但是和其他人一样,你也可以从一系列的词开始。

参见文档:http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split%28java.lang.String%29

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

+0

您绝对正确,我在没有Eclipse的课程中编写了该脚本,由于我对该语言非常陌生,因此我明白我确实需要一个IDE。 – 1vannn 2013-05-14 18:57:02

3

如果你想要使用计数最高的单词,那么你的问题不应该是一个随机的单词。它应该是有关长排序的话,那么选择来自前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()); 
     } 
    } 

选择一个随机字

Random numGen= new Random(); 
String word = words [numGen.nextInt(words.size)]; 

对于一个实际有效的排序,你需要编写自己的比较单词(基于长度,但对于较长的单词优先),然后创建一个PriorityQueue,在其中添加单词,当您执行remove()时,将得到最长单词。

+0

再次,每个单词相同的赔率 – 2013-05-14 18:19:05

+0

@MrD你有什么建议呢? – Gamb 2013-05-14 18:23:56

+0

@Gamb一个方法,回答提问者的问题,我正在 – 2013-05-14 18:25:59

4

这个答案选择一个随机单词并找到它的长度。

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()); 
1

要获得任何给定String长度只需使用:

string.length(); 

为了得到一个随机字(每个字都取决于它的长度出现不同的机会),首先你需要你的单词添加到某种列表中,这样的:

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

下面的方法将从列表返回一个随机字。越长的话,它有被选择的机会少:

String selectRandomWord(ArrayList<String> words){ 
int lengthOfLongestWord = 200; 
List<Integer> wordsTimesLength = new ArrayList<Integer>(); 

for (int i = 0;i<words.size();i++){ 
    for (int e = 0;e<Math.pow(words.get(i).length,-1)*lengthOfLongestWord;e++){ 

    wordsTimesLength.add(i); 
    } 
} 

int randomIndex = generator.nextInt(wordsTimesLength.size()); 

return words.get(wordsTimesLength.get(randomIndex)); 
} 

注意您需要更改lengthOfLongest,如果你有超过200个字符的文字(我不认为有任何只是在案件)。要使用该方法,你可以简单地调用它是这样的:

selectRandomWord(words); 
0

下面是一些代码,应该为你正在试图做什么工作:

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 
    } 
} 
+0

他的问题是:“如何从变量中获取随机单词”,单词“,并计算它的长度?”我的答案是他的单词和长度 - 其余的由他自己决定 – Haque1 2013-05-14 18:47:38

+0

好点。我的计划是将单词分成困难组,如Hard,Medium和Easy。我不完全确定我会从那里去那里。 – 1vannn 2013-05-14 18:55:00

相关问题