亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將值存儲在數組中并將它們保存在文件中

如何將值存儲在數組中并將它們保存在文件中

森欄 2022-07-27 19:55:02
這是我的代碼的一部分。基本上,我試圖將 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);    }
查看完整描述

2 回答

?
小怪獸愛吃肉

TA貢獻1852條經驗 獲得超1個贊

請將您的代碼修改為:


public class Main {


    public static void main(String[] args) {

        try {

            FileWriter fw = new FileWriter(new File("F://test.txt"));

            Scanner sc = new Scanner(System.in);

            int students = 2;

            double[] doubleAVG = new double[students];

            char[] charGRADE = new char[students];

            double avg = 0.0;

            char gradeLetter;

            String FirstName = "";

            String LastName = "";

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

                System.out.println("Enter average :");

                avg = sc.nextDouble();

                System.out.println("Enter grade :");

                gradeLetter = sc.next().charAt(0);

                System.out.println("Enter First Name :");

                FirstName = sc.next();

                System.out.println("Enter Last Name :");

                LastName = sc.next();

                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] + System.lineSeparator());

            }

            fw.close();

            sc.close();

        } catch (Exception e) {

            System.out.println(e);

        }


    }


}

控制臺輸入:


Enter average :

70.45

Enter grade :

B

Enter First Name :

John 

Enter Last Name :

Johnson

Enter average :

90.47

Enter grade :

A

Enter First Name :

Michael 

Enter Last Name :

Jordan

在test.txt中:


This is the grade of: John Johnson 70.45 B

This is the grade of: Michael Jordan 90.47 A


查看完整回答
反對 回復 2022-07-27
?
拉風的咖菲貓

TA貢獻1995條經驗 獲得超2個贊

您是否知道 java Serialize 方法(學),它創建了您在類文件中擁有的所有數據結構及其對象的 xml 文件,使其中的嵌套結構看起來像特定時間點的對象,就像原始 xml 一樣?這可能是由于 java 在 5 種確切的本機數據類型上中繼,而對象只是這些數據類型的集合??纯瓷厦娴脑u論以及 Stackoverflow 答案的鏈接,我只是向您解釋了一些有關機制的信息。



查看完整回答
反對 回復 2022-07-27
  • 2 回答
  • 0 關注
  • 113 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號