【发布时间】:2014-04-28 09:49:58
【问题描述】:
我正在编写一个处于 while 循环中的程序,它将随机数输入到队列中。当队列已满时,它会将其写入文本文件并再次填满队列。到目前为止它很好,但我想做的是当用户在控制台中输入单词“stop”时,while循环停止。我该如何解决这个问题?如您所知,我知道我在下面使用的方法不起作用。
public static void main (String [] args) throws IOException{
DataBuffert theBuffert = new DataBuffert(10);
Random random = new Random();
DataOutputStream logFile = new DataOutputStream(new FileOutputStream("logfile.txt"));
while(!System.in.equals("stop")){
theBuffert.enqueue(random.nextInt(500));
if (theBuffert.isFull()){
while (!theBuffert.isEmpty()){
logFile.writeBytes(String.valueOf(theBuffert.dequeue()+"\n"));
try {
Thread.sleep(1000);
} catch(InterruptedException ex) {
System.out.println("Operation interrupted");
}
}
}
}
}
【问题讨论】:
-
您将
InputStream与String进行比较