此代码用于将插入的数据写入文件
当循环运行时,第二个学生没有进入,我不知道为什么
这一直困扰着第二个学生,而我已经为此工作了几个小时

import java.util.Scanner;
import java.io.*;

class wonder
{
public static void main(String args[]) throws IOException
{
    Scanner c = new Scanner(System.in);

    FileWriter ww = new FileWriter("D:\\student details.txt");
    BufferedWriter o = new BufferedWriter(ww);

    int counter = 0 ;
    int stnum = 1;

    while(counter < 4)
    {
        String name = "";
        int marks = 0;

        System.out.print("Enter student " + stnum + " name : " );
        name = c.nextLine();

        System.out.print("Enter marks : ");
        marks = c.nextInt();

        ww.write(stnum + " " + name + " " + marks );
        ww.close();

        counter++;
        stnum++;

    }


}
}

最佳答案

您在while循环的每个迭代中关闭FileWriter ...您认为会发生什么?

10-08 17:05