【发布时间】:2019-12-16 14:25:01
【问题描述】:
我是一个java初学者,我有一个小问题,我希望你们能帮助我。 我有一个包含大量随机名称的 Names.txt 文件,每一行都有一个适当的名称 (解释: 约翰 迈克尔 安东尼 ETC...) 我一直在编写一个随机选择以下名称之一的函数:
public static void RandomNameGenerator() throws FileNotFoundException
{
// the File.txt has organized names, meaning that each line contains a name
//the idea here is to get a random int take that number and find a name corresponding to that line number
int txtnumlines = 0; // how many lines the txt file has
Random random = new Random();
Scanner file = new Scanner(new File("Names.txt")); //loads the txt file
while (file.hasNext()) //counts the number of lines
{
file.nextLine();
txtnumlines += 1;
}
int randomintname = random.nextInt(txtnumlines);
// takes a random number, that number will be used to get the name from the txt line
String RandomName = "";
// I'm stuck here :(
}
问题是我不知道如何继续,更具体地说,如何使用代表随机线的随机整数提取该名称(比如说 alex)
希望我的问题很清楚, 谢谢你帮助我!
【问题讨论】:
-
特别是得分最高的答案,而不是接受的答案
-
谢谢你中间的先生,它现在工作了:)
标签: java