1. 程式人生 > >有五個學生,每個學生有3門課的成績, 從鍵盤輸入以上資料(包括姓名,三門課成績), 輸入的格式:如:zhagnsan,30,40,60計算出總成績, 並把學生的資訊和計算出的總分數高低順序存放在磁碟文

有五個學生,每個學生有3門課的成績, 從鍵盤輸入以上資料(包括姓名,三門課成績), 輸入的格式:如:zhagnsan,30,40,60計算出總成績, 並把學生的資訊和計算出的總分數高低順序存放在磁碟文

有五個學生,每個學生有3門課的成績,
從鍵盤輸入以上資料(包括姓名,三門課成績),
輸入的格式:如:zhagnsan,30,40,60計算出總成績,
並把學生的資訊和計算出的總分數高低順序存放在磁碟檔案"stud.txt"中。
1:定義一個描述學生的類

2定義一個操作學生的工具類.0

//比較棒的程式,用到了流讀寫檔案,集合泛型

import java.io.*;
import java.util.*;
class Student implements Comparable<Student>
{
    private String name;
    private int shu,yu,eng;
    private int sum;

    public Student(){}

    public Student(String name,int shu,int yu,int eng)
    {
        this.name = name;
        this.shu = shu;
        this.yu = yu;
        this.eng = eng;
        sum = shu+yu+eng;
    }

    public int compareTo(Student stu)
    {
        int num = this.sum-stu.sum;
        return num==0?this.name.compareTo(stu.name):num;
    }
    public String getName()
    {
        return name;
    }
    public int getShu()
    {
        return shu;
    }
    public int getYu()
    {
        return yu;
    }
    public int getEng()
    {
        return eng;
    }
    public int getSum()
    {
        return sum;
    }
    public String toString()
    {
        return name+","+shu+","+yu+","+eng;
    }
}
class StuTool
{
    public static Set<Student> getStudents() throws IOException
    {
        return getStudents(null);
    }

    //從鍵盤獲取學員資訊,存到集合中
    public static Set<Student> getStudents(Comparator<Student> com)throws IOException
    {
        TreeSet<Student> ts = null;
        if(com==null)
            ts = new TreeSet<>();
        else
            ts = new TreeSet<>(com);

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = null;
        for(int i=1;i<=3;i++)
        {  
            System.out.println("請輸入第"+i+"個學員的資訊");
            line = br.readLine();
            String[] arr = line.split(",");
            ts.add(new Student(arr[0],Integer.parseInt(arr[1]),Integer.parseInt(arr[2]),Integer.parseInt(arr[3])));
        }
        br.close();
        return ts;
    }
    //把學員資訊 按照成績從高到低寫入到檔案中
    public static void writeInfo(Set<Student> stus)throws IOException
    {
        BufferedWriter bw = new BufferedWriter(new FileWriter("stud.txt"));
        for(Student stu:stus)
        {
            bw.write(stu.toString()+"    ");
            bw.write(""+stu.getSum());
            bw.newLine();
            bw.flush();
        }
        bw.close();
    }
}

class n1
{
    public static void main(String[] args) throws IOException
    {
        Comparator<Student> com = Collections.reverseOrder();

        Set<Student> stus = StuTool.getStudents(com);
        //但是這一句採用由低到高的預設順序
        //    Set<Student> stus = StuTool.getStudents();

        StuTool.writeInfo(stus);


    }

}

//採用更簡明的解法

import java.io.*;
import java.util.*;

 class Student {
        private String name;
        private int chinese;
        private int math;
        private int english;
        private int sum;
        
        public Student(){}
        
        public Student(String name, int chinese, int math, int english){
                super();
                this.name = name;
                this.chinese = chinese;
                this.math = math;
                this.english = english;
                this.sum = this.chinese + this.math + this.english;
        }
        
        public int getSum(){
                return sum;
        }
        
        public String toString(){
                return name+","+chinese+","+math+","+english+",總成績:"+sum;
                
        }        
}

class ComBySum implements Comparator<Student>
{
    public int compare(Student t1, Student t2)
    {
        int num=t1.getSum()-t2.getSum();
        return num;
    }
}


public class n1 {

        public static void main(String[] args) throws IOException
            {
                getMassage();
           }
        
        public static void getMassage() throws IOException{
                Scanner sc = new Scanner(System.in);
                System.out.println("請輸入學生資訊,輸入格式為:name,30,30,30");

                ComBySum comBySum=new ComBySum();
                TreeSet<Student> ts = new TreeSet<>(comBySum);
                for(int i=0;i<5;i++)
                    {
                        String line = sc.nextLine();
                        String[] arr;
                        int chinese;
                        int math;
                        int english;
                        try {
                                arr = line.split("\\,");
                                chinese = Integer.parseInt(arr[1]);
                                math = Integer.parseInt(arr[2]);
                                english = Integer.parseInt(arr[3]);
                                ts.add(new Student(arr[0],chinese,math,english));
                        } catch (NumberFormatException e) {
                                System.out.println("錄入格式錯誤,請重新錄入。。。。");        
          }
                }
                BufferedWriter bw = new BufferedWriter(new FileWriter("stu.txt"));
                for(Student s : ts)
                {
                        bw.write(s.toString());
                        bw.newLine();
                }
                bw.close();        
             }
}