亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java實(shí)現(xiàn)學(xué)生成績(jī)輸出到磁盤文件的方法詳解

 更新時(shí)間:2022年11月01日 10:42:16   作者:小虛竹and掘金  
這篇文章主要為大家詳細(xì)介紹了如何利用Java實(shí)現(xiàn)將學(xué)生成績(jī)輸出到磁盤文件的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下

一、題目描述

題目:有五個(gè)學(xué)生,每個(gè)學(xué)生有 3 門課的成績(jī),從鍵盤輸入以上數(shù)據(jù)(包括學(xué)生號(hào),姓名,三門課成績(jī)),

把這些數(shù)據(jù)存放在磁盤文件 "stud.txt "中。

二、解題思路

1、寫學(xué)生鍵盤輸入和成績(jī)鍵盤輸入,Scanner input1 = new Scanner(System.in);

2、把學(xué)生和成績(jī)拼接成字符串

3、把字符串保存到硬盤文件 "stud.txt "中。

三、代碼詳解

public class Basics102 {
    public static void  fileWriter(String str)
    {
        FileWriter fw =null;
        try {
             fw = new FileWriter("D:\\stud.txt", true);
            //如["\\stud.txt"]則表示在項(xiàng)目盤符的根目錄建立文件,如項(xiàng)目在F盤,則在F盤根目錄建立文件
            //如["save\\stud.txt"]則表示在當(dāng)前項(xiàng)目文件夾里找到命名為[save]的文件夾,把文件新建在該文件夾內(nèi)
            System.out.println("數(shù)據(jù)已成功寫入");
            fw.write(str);
            fw.close();
        } catch (Exception e) {
            //拋出一個(gè)運(yùn)行時(shí)異常(直接停止掉程序)
            throw new RuntimeException("運(yùn)行時(shí)異常",e);
        }finally {
            try {
                //操作完要回收流
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {

        int i,j,k=1;
        String id[] = new String[5];
        int score[] = new int[15];
        String name[] =  new String[5];
        String str=null;

        Scanner inputId = new Scanner(System.in);
        Scanner inputName = new Scanner(System.in);
        Scanner inputScore = new Scanner(System.in);

        for(i=0;i<5;i++)
        {
            System.out.print("請(qǐng)輸入第"+(i+1)+"位同學(xué)的學(xué)號(hào):");
            id[i]=inputId.nextLine();
            System.out.print("請(qǐng)輸入第"+(i+1)+"位同學(xué)的姓名:");
            name[i]=inputName.nextLine();
            for(j=i*3;j<i*3+3;j++)
            {
                System.out.print("請(qǐng)輸入第"+(i+1)+"位同學(xué)的第"+k+++"門成績(jī):");
                score[j]=inputScore.nextInt();

            }
            k=1;
        }

        for(i=0;i<5;i++)
        {
            str="學(xué)號(hào)"+id[i];
            str=str+" "+"姓名"+name[i];

            for(j=i*3;j<i*3+3;j++)
                str=str+" "+"第"+k+++"門成績(jī)="+score[j];
            k=1;
            System.out.println();
            fileWriter(str+"\r\n");
        }
    }
}

解法二:引入Hutool

解題思路

1、寫學(xué)生鍵盤輸入和成績(jī)鍵盤輸入,Scanner input1 = new Scanner(System.in);

2、把學(xué)生和成績(jī)拼接成字符串

3、把字符串保存到硬盤文件 "stud.txt "中。

在上一個(gè)解法的基礎(chǔ)上,優(yōu)化了第三步,使用

將String寫入文件,UTF-8編碼追加模式

FileUtil.appendUtf8String(str,"D:\stud2.txt");

代碼詳解

public class Basics102_2 {

    public static void main(String[] args) {

        int i,j,k=1;
        String id[] = new String[5];
        int score[] = new int[15];
        String name[] =  new String[5];
        String str=null;

        Scanner inputId = new Scanner(System.in);
        Scanner inputName = new Scanner(System.in);
        Scanner inputScore = new Scanner(System.in);

        for(i=0;i<5;i++)
        {
            System.out.print("請(qǐng)輸入第"+(i+1)+"位同學(xué)的學(xué)號(hào):");
            id[i]=inputId.nextLine();
            System.out.print("請(qǐng)輸入第"+(i+1)+"位同學(xué)的姓名:");
            name[i]=inputName.nextLine();
            for(j=i*3;j<i*3+3;j++)
            {
                System.out.print("請(qǐng)輸入第"+(i+1)+"位同學(xué)的第"+k+++"門成績(jī):");
                score[j]=inputScore.nextInt();

            }
            k=1;
        }

        for(i=0;i<5;i++)
        {
            str="學(xué)號(hào)"+id[i];
            str=str+" "+"姓名"+name[i];

            for(j=i*3;j<i*3+3;j++)
                str=str+" "+"第"+k+++"門成績(jī)="+score[j];
            str +="\n";
            k=1;
            try {
                //不需要關(guān)閉文件流,源碼已經(jīng)有了
                FileUtil.appendUtf8String(str,"D:\\stud2.txt");
            }catch (IORuntimeException e){
                //拋出一個(gè)運(yùn)行時(shí)異常(直接停止掉程序)
                throw new RuntimeException("運(yùn)行時(shí)異常",e);
            }
        }
    }
}

到此這篇關(guān)于Java實(shí)現(xiàn)學(xué)生成績(jī)輸出到磁盤文件的方法詳解的文章就介紹到這了,更多相關(guān)Java成績(jī)輸出到磁盤文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論