2013-05-14 79 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

相關問題