场景:我将把一天中的提示作为作业,我尝试了很多并且可以达到以下水平: 我的问题是如何在当前日期从文件库生成一个语句,如果一个语句已经显示当前日期,不再显示,从文件中获取另一个随机语句。 P.S:抱歉我的英语不好
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
class FileRead {
public static void main(String args[])
{
ArrayList<String> tip = new ArrayList<String>();
Random r= new Random();
int n= r.nextInt(10);
try{
FileInputStream fstream = new FileInputStream("d:/MyPhrases.txt");
// use DataInputStream to read binary NOT text
// DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// write the content on the arraylist
tip.add(br.readLine());
}
//Close the input stream
in.close();
// Print the content on the console
while (tip != null) {
//Collections.shuffle(tip);
System.out.println (tip.get(n).toString());
System.exit(0);
}
}catch (Exception e){
System.err.println("Error: " + e.getMessage());
}
}
}
答案 0 :(得分:0)
对于这一行,
tip.add(br.readLine());
我相信你想要
tip.add(strline);
如果您使用了第一行,则列表将添加下一行,而不是当前行。
修改
while ((strLine = br.readLine()) != null && i <= n) {
if (!tip.contains(strLine)) {
tip.add(strLine);
break;
}
n++;
}