2014-12-05 73 views
2

我想製作一個Mad Libs程序,您可以在其中編寫一個瘋狂的libs模板,並且計算機會爲您填充空白。我得到這個至今:從文本文件中返回一個隨機行

package madlibs; 
import java.io.*; 
import java.util.Scanner; 

/** 
* 
* @author Tim 
*/ 
public class Madlibs { 

/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) throws IOException { 
    File nouns = new File("nounList.txt"); 
    Scanner scan = new Scanner(nouns); 
    while(scan.hasNextLine()){ 
     if("__(N)".equals(scan.nextLine().trim())){ 
      int word = (int) (Math.random() * 100); 

     } 
    } 
} 

} 

nounList.txt文件包含名詞列表,每一個單獨的行。問題是:如何使用Math.random函數然後選擇使用哪條線?

+2

平面文件不擅長隨機訪問。您的選擇將是讀取整個文件並隨機選擇一個文件,或逐行讀取,直到您到達隨機選擇的索引。我建議使用數據庫。考慮sqlite。 – Andreas 2014-12-05 22:36:49

回答

1

獲取列表中的所有名詞,然後從列表中選擇一個隨機元素。

例子:

// Nouns would contain the list of nouns from the txt file 
List<String> nouns = new ArrayList<>(); 
Random r = new Random(); 
String randomNoun = nouns.get(r.nextInt(0, nouns.length)); 
+1

如果文件很大,該怎麼辦?更好的解決方案是使用隨機訪問。 – 2014-12-05 22:39:07

+0

There's 455 words – Tim 2014-12-05 22:43:49

+0

只適用於455個單詞。 – MCMastery 2014-12-06 15:10:27

0

我會做的另一種方法由我看到一個評論的建議:

try { 
     //I would prefer to read my file using NIO, which is faster 
     Path pathToMyTextFile = Paths.get("nounList.txt"); 
     //Then I would like to obtain the lines in Array, also I could have them available for process later 
     List<String> linesInFile = Files.readAllLines(pathToMyTextFile, StandardCharsets.ISO_8859_1); 
     //If I want to access a random element, I would use random methods to access a random index of the list and retrieve that element 
     Random randomUtil = new Random(); 

     //I will use the formula for random and define the maximum (which will be the length of the array -1) and the minimum which will be zero 
     //since the indexes are represented from 0 to length - 1 
     int max = linesInFile.size() - 1; 
     int min = 0; 

     //You can simplify this formula later, I'm just putting the whole thing 
     int randomIndexForWord = randomUtil.nextInt((max - min + 1)) + min; 

     //Here I get a random Noun 
     String randomWord = linesInFile.get(randomIndexForWord); 

    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

這將是另一種方式來做到這一點,而不需要訪問該的每次你需要一個名詞。

問候和...編碼快樂:)

0

主要有兩大任務:

閱讀所有的名詞的

// Open the file 
    File file = new File("MyFile.txt"); 

    // Attach a scanner to the file 
    Scanner fin = new Scanner(file); 

    // Read the nouns from the file 
    ArrayList<String> nouns = new ArrayList<>(); 
    while (fin.hasNext()) { 
     nouns.add(fin.next()); 
    } 

隨機

選擇一個
// Pick one at random 
    int randomIndex = (int)(Math.random() * nouns.size()); 
    String randomNoun = nouns.get(randomIndex); 

    // Output the result 
    System.out.println(randomNoun); 

例如,如果我們有10個名詞,那麼Math.random() * 10產生範圍從0.0到9.999 ... 9。投射到int會截斷小數點,在0到9之間保留相等的分佈。

請注意,您可以在技術上擲出一個完美的10.0,並且該程序會崩潰,並顯示IndexOutOfBoundsException。這在統計上是不可能發生的,但正如我們都知道的那樣,統計上不可能在代碼中不夠好。考慮添加邏輯來處理10.0版本的情況。

相關問題