有没有办法输出到多个方法的文件?我在C ++中使用了这种技术:
// create Scanner for input
static Scanner in = new Scanner(System.in);
// Output survey results to a file
public static void main(String[] args) throws Exception
{
// create Formatter for output
Formatter out = new Formatter("numbers.txt");
// store answer
int ans;
// ask 40 students to rate the food at the cafeteria
// 1 is bad, 10 is good
for(int i = 0; i < 40; i++) {
// ensure answer is between 1 and 10
do {
System.out.print("Rate the food at the cafeteria between 1 (horrid) and 10 (excellent): ");
ans = in.nextInt();
} while(ans > 10 || ans < 0);
// end do-while
} // end for
// close file
out.close();
} // end method main
// Output data
public static void output(Formatter out, int num)
{
// enter data in a file
out.format("%d\n", num);
} // end method output
它编译,运行,创建文件,等等;但是当我打开文件时,如果是空的。任何建议将不胜感激。
答案 0 :(得分:4)
问题是output()
写了文件,但你永远不会打电话给output()
。因此,永远不会写入文件。你需要插入:
output(out, ans);