如何将值存储在数组中并将它们保存在文件中

这是我的代码的一部分。基本上,我试图将 double Avg 和 chargradeLetter 的值存储在数组 doubleAVG 和 charGRADE 中。到目前为止,我得到的是在第一个循环中,假设 Avg = 2 那么它将存储在文件中,但是如果有第二个循环并且 Avg 的值更改为 3 则 3 将保存在文件中两次。它删除 2 并保存数字 3 两次。我该如何解决?我希望第一个循环存储可以为 2 的 Avg 的第一个值,然后在第二个循环上存储可以为 3 但不覆盖 2 的 Avg 的新值。忽略评论。


    try {

        FileWriter fw = new FileWriter(fileN);


        for (int count = 0; count < students; count++) {


            doubleAVG = new double[students];

            charGRADE = new char[students];


            doubleAVG[count] = Avg;

            charGRADE[count] = gradeLetter;


            fw.write("This is the grade of: " + FirstName + " " + LastName

                    + " ");

            fw.write(String.valueOf(doubleAVG[count]) + " ");

            fw.write(charGRADE[count]);

        }

        fw.close();

    } catch (Exception e) {

        System.out.println(e);

    }



森栏
浏览 90回答 2
2回答

小怪兽爱吃肉

请将您的代码修改为:public class Main {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FileWriter fw = new FileWriter(new File("F://test.txt"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Scanner sc = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int students = 2;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double[] doubleAVG = new double[students];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char[] charGRADE = new char[students];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double avg = 0.0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char gradeLetter;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String FirstName = "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String LastName = "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int count = 0; count < students; count++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter average :");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; avg = sc.nextDouble();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter grade :");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gradeLetter = sc.next().charAt(0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter First Name :");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FirstName = sc.next();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter Last Name :");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LastName = sc.next();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; doubleAVG[count] = avg;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; charGRADE[count] = gradeLetter;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fw.write("This is the grade of: " + FirstName + " " + LastName + " ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fw.write(String.valueOf(doubleAVG[count]) + " ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fw.write(charGRADE[count] + System.lineSeparator());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fw.close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sc.close();&nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(e);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}控制台输入:Enter average :70.45Enter grade :BEnter First Name :John&nbsp;Enter Last Name :JohnsonEnter average :90.47Enter grade :AEnter First Name :Michael&nbsp;Enter Last Name :Jordan在test.txt中:This is the grade of: John Johnson 70.45 BThis is the grade of: Michael Jordan 90.47 A

拉风的咖菲猫

您是否知道 java Serialize 方法(学),它创建了您在类文件中拥有的所有数据结构及其对象的 xml 文件,使其中的嵌套结构看起来像特定时间点的对象,就像原始 xml 一样?这可能是由于 java 在 5 种确切的本机数据类型上中继,而对象只是这些数据类型的集合。看看上面的评论以及 Stackoverflow 答案的链接,我只是向您解释了一些有关机制的信息。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java