【发布时间】:2016-02-10 04:31:33
【问题描述】:
我可以从文件中读取,并且能够通过更改 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);
}
}
}
【问题讨论】:
-
请解释清楚你的意思。
-
输出是所有并排的数字,然后生成我分配的许多列。我需要让数字随机下降,直到有 20。我的 txt 文件是 20 'enter' 10 'enter' 15 等等
标签: java