第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

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

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

森欄 2022-07-27 19:55:02
這是我的代碼的一部分?;旧?,我試圖將 double Avg 和 chargradeLetter 的值存儲在數(shù)組 doubleAVG 和 charGRADE 中。到目前為止,我得到的是在第一個(gè)循環(huán)中,假設(shè) Avg = 2 那么它將存儲在文件中,但是如果有第二個(gè)循環(huán)并且 Avg 的值更改為 3 則 3 將保存在文件中兩次。它刪除 2 并保存數(shù)字 3 兩次。我該如何解決?我希望第一個(gè)循環(huán)存儲可以為 2 的 Avg 的第一個(gè)值,然后在第二個(gè)循環(huán)上存儲可以為 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貢獻(xiàn)1852條經(jīng)驗(yàn) 獲得超1個(gè)贊

請將您的代碼修改為:


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


查看完整回答
反對 回復(fù) 2022-07-27
?
拉風(fēng)的咖菲貓

TA貢獻(xiàn)1995條經(jīng)驗(yàn) 獲得超2個(gè)贊

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



查看完整回答
反對 回復(fù) 2022-07-27
  • 2 回答
  • 0 關(guān)注
  • 131 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號