2014-12-05 40 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版本的情况。

相关问题