zoukankan      html  css  js  c++  java
  • /* * 有五个学生,每个学生有3门课的成绩,从键盘输入以上数据 *(包括学生号,姓名,三门课成绩),计算出平均成绩, *将原有的数据和计算出的平均分数存放在磁盘文件"stud"中。 */

    1.Student类:类中有五个变量,分别是学号,姓名,三门成绩

    package test3;
    public class Student {
        private int num;
        private String name;
        private  double Chinese;
        private double English;
        private double math;
        private double avg;
        public Student() {
            
        }
        @Override
        public String toString() {
            return "学生学号:" + num + ", 姓名:" + name + ", 语文成绩:" + Chinese + ", 英语成绩:" + English + ", 数学成绩:"
                    + math + ",平均成绩:" + avg ;
        }
        public int getNum() {
            return num;
        }
        public void setNum(int num) {
            this.num = num;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public double getChinese() {
            return Chinese;
        }
        public void setChinese(double chinese) {
            Chinese = chinese;
        }
        public double getEnglish() {
            return English;
        }
        public void setEnglish(double english) {
            English = english;
        }
        public double getMath() {
            return math;
        }
        public void setMath(double math) {
            this.math = math;
        }
        public void setAvg(double avg) {
            this.avg=avg;
        }
        public double getAvg() {
            return avg;
        }
    }

    2.StudentDao类,用来获取学生的平均成绩 的工具类

    package test3;
    
    public class StudentDao {
        static Student stu;
        static double avg;
       public static double getAvg(Student s) {
           stu=s;
           double cw;
           double en;
           double ma;
           
           cw=stu.getChinese();
           en=stu.getEnglish();
           ma=stu.getMath();
           
           avg=(cw+en+ma)/3;
           return avg;
       }
    }

    3.主类,用来获取学生的信息和把学生的信息输入到本地文件stud中

    package test3;
    
    import java.io.BufferedOutputStream;
    import java.io.BufferedWriter;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.Scanner;
    
    /*
     * 有五个学生,每个学生有3门课的成绩,从键盘输入以上数据
     *(包括学生号,姓名,三门课成绩),计算出平均成绩,
     *将原有的数据和计算出的平均分数存放在磁盘文件"stud"中。
     */
    public class test {
        static ArrayList<Student> list = new ArrayList<Student>();
        static BufferedWriter out = null;
    
        public static void main(String[] args) throws Exception {
            getStu();
            saveStu();
        }
    
        // 从键盘获取学生信息
        public static void getStu() {
            Scanner scan = new Scanner(System.in);
            int count = 1;
            while (list.size() < 3) {
                System.out.println("--------请输入学生信息--------");
                System.out.println("请输入" + count + "学生的学号:");
                int num = scan.nextInt();
                System.out.println("请输入" + count + "学生的姓名:");
                String name = scan.next();
                System.out.println("请输入" + count + "学生的语文成绩:");
                double cw = scan.nextDouble();
                System.out.println("请输入" + count + "学生的数学成绩:");
                double sx = scan.nextDouble();
                System.out.println("请输入" + count + "学生的英语成绩:");
                double yy = scan.nextDouble();
    
                Student s = new Student();
                // 判断学生成绩是否正确
                if (!(cw < 0) && !(yy < 0) && !(sx < 0)) {
                    s.setNum(num);
                    s.setName(name);
                    s.setChinese(cw);
                    s.setEnglish(yy);
                    s.setMath(sx);
                    s.setAvg(StudentDao.getAvg(s));
                    list.add(s);
                    count += 1;
    
                } else {
                    System.out.println("成绩输入错误!");
                    continue;
                }
            }
            System.out.println(list);
        }
    
        // 把学生信息录入本地文件
        public static void saveStu() {
            // 创建一个字符缓冲输出流
            try {
                out = new BufferedWriter(new FileWriter("stud.txt"));
                for (int i = 0; i < list.size(); i++) {
                    out.write("姓名:" + list.get(i).getName() + " 学号:" + list.get(i).getNum() + " 语文成绩:"
                            + list.get(i).getChinese() + " 数学成绩" + list.get(i).getMath() + "英语成绩:"
                            + list.get(i).getEnglish() + " 平均成绩:" + list.get(i).getAvg());
                    out.newLine();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (out != null) {
                        out.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    
        }
    }

    /*
    * 带缓冲区的流中的特殊方法
    * readLine()属于BufferedReader
    * newLine()属于BufferedWriter
    *
    * newLine 和 \r\n的区别
    * newLine是跨平台的方法
    * \r\n只支持的是windows系统
    */
    ---------------------
    作者:Soar_Sir
    来源:CSDN
    原文:https://blog.csdn.net/SoarFly0807/article/details/76034807
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    Netty回调与Channel执行流程分析
    Netty执行流程分析与重要组件介绍
    HBase 介绍
    Java文件上传下载原理
    ngxtop安装和使用
    开启Nginx监控 with-http_stub_status_module
    Spring 事务模板方法设计模式
    Spring 事务管理
    JdkDynamicAopProxy 拦截器链的获得与递归执行
    CgLib实现AOP
  • 原文地址:https://www.cnblogs.com/zhilili/p/10671683.html
Copyright © 2011-2022 走看看