我正在java中構建一個小軟件來測試函數和PrintWriter方法。但是當我運行它時,只會打印循環的最後一個數字。例如在奇數文件上只打印99,在偶數文件上只打印100個。打印文件編寫器只寫1個編號
我創建了幾個system.out.println來測試循環是否正常工作,它看起來像是。有誰知道它爲什麼只打印一行?
/**
*
* @author bertadevant
*/
import java.io.*;
public class Filewritermethods {
public static void main(String[] args) throws IOException {
Numbers();
}
public static void Numbers() throws IOException {
for (int i =1; i<=100; i++){
EvenOdd(i);
}
}
public static void EvenOdd (int n) throws IOException {
File Odd = new File ("odd.txt");
File Even = new File ("even.txt");
File All = new File ("all.txt");
PrintWriter all = new PrintWriter (All);
all.println(n);
all.close();
if (n%2==0){
PrintFile(Even, n);
System.out.println ("even");
}
else {
PrintFile (Odd, n);
System.out.println ("odd");
}
}
public static void PrintFile (File filename, int n) throws IOException {
PrintWriter pw = new PrintWriter (filename);
if (n!=0) {
pw.println(n);
System.out.println (n + " printfile method");
}
else {
System.out.println ("The number is not valid");
}
pw.close();
}
}