zoukankan      html  css  js  c++  java
  • 【Java例题】7.5 文件题2-学生成绩统计


    5.学生成绩统计。
    已有一个学生成绩文件,含有多位学生的各三门课的成绩;
    读取这个文件中的每位学生的三门课成绩,然后计算均分;
    最后对这些均分按照大于或小于75分的界限,分别写到另两个文件中。

    package chapter7;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.PrintStream;
    import java.util.Scanner;
    
    public class demo5 {
        public static void main(String[] args) {
            try {
                student stu[]=new student[10];
                for(int i=0;i<10;i++) {
                    stu[i]=new student();//必须挨个实例化对象数组
                }
                Scanner sc=new Scanner(new File("score1.txt"));
                PrintStream out1=new PrintStream(new File("score3.txt"));
                PrintStream out2=new PrintStream(new File("score4.txt"));
                
                for(int i=0;i<10;i++) {
                    if(sc.hasNext()) {
                        stu[i].name=sc.next();
                        stu[i].sco1=Integer.parseInt(sc.next());
                        stu[i].sco2=Integer.parseInt(sc.next());
                        stu[i].sco3=Integer.parseInt(sc.next());
                    }
                }
                for(int i=0;i<10;i++) {
                    if(stu[i].name==null) {
                        break;
                    }
                    if(stu[i].getarg()>=75) {
                        out1.println(stu[i].name);
                    }else {
                        out2.println(stu[i].name);
                    }
                }
                out1.close();
                out2.close();
                sc.close();
            }catch(FileNotFoundException e) {
                System.out.println("file not found");
            }catch(Exception e) {
                System.out.println(e);
            }
        }
        
        static class student{
            String name;
            int sco1;
            int sco2;
            int sco3;
            public int getarg() {//用函数定义arg而不是int arg=sco1+sco2+sco3;因为这样不初始化sco123,调用arg时默认为零
                return (sco1+sco2+sco3)/3;
            }
        }
    }
  • 相关阅读:
    牛逼的博客地址
    动画的keyPath
    跳转到系统设置的各种配置
    UITextField只允许输入正数
    冒泡排序
    number类型的数组
    正则表达式
    C中常用的数学函数
    利用运行时,查看一个类的所有子类
    玉蟾宫(悬线法)
  • 原文地址:https://www.cnblogs.com/LPworld/p/10724110.html
Copyright © 2011-2022 走看看