當我在Java控制檯中打印出來時,下面的程序完美地工作,但是當我嘗試追加要放入文本文件的程序時,它僅將1/5的學生平均值打印到附加的文本文件中。如何將java輸出附加到文本文件?
Bobby, average = 93
我想這是所有印刷5學生的平均水平爲提前讓
Agnes, average = 76
Bufford, average = 91
Julie, average = 94
Alice, average = 39
Bobby, average = 93
感謝。
每次它進入這一行時間import java.util.*;
import java.io.*;
public class StudentAverage {
public static void main(String[]args) throws IOException {
Scanner scanner = new Scanner(new
File("D:\\School\\StudentGrades.txt"));
while (scanner.hasNextLine()) {
Scanner scanners = new Scanner(scanner.nextLine());
String name = scanners.next();
double total = 0;
int num = 0;
while (scanners.hasNextInt()) {
total += scanners.nextInt();
num++;
}
PrintStream output = new PrintStream (("D:\\School\\WriteStudentAverages.txt"));
output.print(name + ", average = " + (Math.round(total/num)));
output.flush();
}
scanner.close();
}
}
謝謝!很棒。 –