录入学生成绩并写出到磁盘文件

来源:互联网 发布:大型投资理财网站源码 编辑:程序博客网 时间:2024/06/08 18:30
package com.ibeifeng;import java.io.BufferedWriter;import java.io.FileWriter;import java.io.IOException;import java.util.Scanner;/* * 4题目:有五个学生,每个学生有3门课的成绩, * 从键盘输入以上数据(包括学生号,姓名,三门课成绩), * 计算出平均成绩, * 把原有的数据和计算出的平均分数存放在磁盘文件 "student.txt "中。(15分)*/public class Demo4 {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        BufferedWriter bw=null;        try {             bw = new BufferedWriter(new FileWriter("student.txt"));        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        try {            bw.write("学号-姓名-语文-数学-英语-平均分");            bw.newLine();            bw.flush();        } catch (IOException e1) {            // TODO Auto-generated catch block            e1.printStackTrace();        }        boolean flag = true;        while(flag) {            System.out.println("请输入学生姓名");            String name = sc.next();            System.out.println("请输入学生学号");            String num = sc.next();            System.out.println("请输入学生chinese");            double chinese = sc.nextDouble();            System.out.println("请输入学生math");            double math = sc.nextDouble();            System.out.println("请输入学生english");            double english = sc.nextDouble();            Student stu = new Student(name,num,chinese,math,english);            System.out.println("别输入了,(y/n)");            if("n".equalsIgnoreCase(sc.next())) {                flag=false;            }            //写入文件            try {                bw.write(stu.toString());                bw.newLine();                bw.flush();            } catch (IOException e) {                e.printStackTrace();            }        }        //关闭        if(bw!=null) {            try {                bw.close();            } catch (IOException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }        System.out.println("over");    }}class Student {    private String name;    private String num;    private double chinese;    private double math;    private double english;    private double avg;    public Student(String name, String num, double chinese, double math,            double english) {        super();        this.name = name;        this.num = num;        this.chinese = chinese;        this.math = math;        this.english = english;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getNum() {        return num;    }    public void setNum(String num) {        this.num = num;    }    public double getChinese() {        return chinese;    }    public void setChinese(double chinese) {        this.chinese = chinese;    }    public double getMath() {        return math;    }    public void setMath(double math) {        this.math = math;    }    public double getEnglish() {        return english;    }    public void setEnglish(double english) {        this.english = english;    }    public double getAvg() {        return (chinese+math+english)/3;    }    @Override    public String toString() {        return num+"-"+name+"-"+chinese+"-"+math+"-"+english+"-"+getAvg();    }}
0 0
原创粉丝点击