【Java例題】7.5ファイル問題2-学生成績統計

4780 ワード

5.学生の成績統計。もう一人の学生の成績書類があります。複数の学生の各三課の成績が含まれています。この書類の中の学生一人の三課の成績を読んで、均等点を計算します。最後に、これらの均等分割については、75点以上の境界線に従って、それぞれ別の2つのファイルに記入します。
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;
        }
    }
}
 
転載先:https://www.cnblogs.com/LPworld/p/10724110.html