我正在用 java 构建一个小软件来测试功能和 PrintWriter 方法。但是当我运行它时,只打印循环的最后一个数字。例如,奇数文件仅打印 99,偶数文件仅打印 100。
我创建了几个 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();
}
}