【发布时间】:2020-07-06 04:29:22
【问题描述】:
以下是我到目前为止的代码。我几周前才开始编程,所以我对这一切都很陌生,我不知道如何随机选择和打印一个单词。我从哪里开始?
public static String randomWord(String fileName)
throws FileNotFoundException {
int fileSize = countWords(fileName);
int N = (int) (fileSize*Math.random());
Scanner inFile = new Scanner(new File(fileName));
String word;
while (inFile.hasNext()) {
word = inFile.next();
}
inFile.close();
return word;
}
【问题讨论】:
-
目前您总是返回文件中的最后一个单词,但您想返回
Nth 单词,对吗?你能想出一个在Nth 字上停下来的方法吗?