我可以从文件中读取内容,并且能够通过更改for循环中的数字来更改行数,但是我不希望那样并排显示文件中的所有数字。我需要它们全部一一随机掉下来。
public class Assignment2 {
public static void main(String[] args) throws IOException
{
// Read in the file into a list of strings
BufferedReader reader = new BufferedReader(new FileReader("textfile.txt"));
List<String> lines = new ArrayList<String>();
String line = reader.readLine();
while( line != null ) {
lines.add(line);
line = reader.readLine();
}
// Choose a random one from the list
Random r = new Random();
for (int p = 0; p<3; p++)
{
String randomString = lines.get(r.nextInt(2));
System.out.println(lines);
}
}
}
我想你要打印的是
String randomString = lines.get(r.nextInt(2));
System.out.println(randomString);
仅显示此列表(可能是100条)中的前20条随机行
for (int i = 0; i < 20; i++) {
int rowNum = r.nextInt(lines.size ());
System.out.println(lines.get(rowNum);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句