日期:2014-05-18  浏览次数:20685 次

★★关于排序的问题★★
这是我编的一个学生成绩管理系统 
我想用根据总分total的高低 将学生信息重新排序并输出 请问该如何写方法 


Java code


import java.io.*;
import java.util.Scanner;
//学生类
class Student{
      public static int count=0;
      private String id;
      private String name;
      private int age;
      private int score1;
      private int score2;
      private int total;
      private int ave;
      private char level;
      private int a=0;
      private int b=0;
      private int c=0;
      private int d=0;      
      public Student(String id,String name,String age,String score1,String score2)      //通过构造方法将外部传入的参数赋值给类的成员变量
        {
         this.id=id;
         this.name=name;
         this.age=Integer.parseInt(age);
         this.score1=Integer.parseInt(score1);
         this.score2=Integer.parseInt(score2);
         }
      public Student(String id,String name,int age,int score1,int score2){
         this.id=id;
         this.name=name;
         this.age=age;
         this.score1=score1;
         this.score2=score2;
      }
      public String GetId(){
         return id;
      }
      public String GetName(){
         return name;
      }
      public int GetAge(){
         return age;
      }
      public int GetScore1(){
         return score1;
      }
      public int GetScore2(){
         return score2;
      }
      public int GetTotal(){
         total=score1+score2; 
         return total;   
      }
     public int GetAve(){
         ave=(score1+score2)/2;
         return ave;
      }
     public char GetLevel(){
         if (ave>89) {level='A';a++;}
            else if (ave>74) {level='B';b++;}
            else if (ave>59) {level='C';c++;}
            else {level='D';d++;}
         return level;
      }   
      public void Display(){
         System.out.println(id+"\t"+name+"\t"+age+"\t"+score1+"\t"+score2+"\t"+total+"\t"+ave+"\t"+level);
         }
      public Student(){
      }
   }

     
//学生成绩管理系统
public class StudentInfo{

   public static final int MAX=100;
   public static Student[] stu=new Student[MAX];

   public static void Output()throws IOException{
      PrintWriter br=new PrintWriter(new BufferedWriter(new FileWriter("student.in")));
      br.println("id\tname\tage\tscore1\tscore2\ttotal");
      for(int i=0;i<Student.count;i++){
         br.println(stu[i].GetId()+"\t"+stu[i].GetName()+"\t"+stu[i].GetAge()+"\t"+stu[i].GetScore1()+"\t"+stu[i].GetScore2()+"\t"+stu[i].GetTotal()+"\t"+stu[i].GetAve()+"\t"+stu[i].GetLevel());
      }
      br.close();
   }

   public static void Input(){
      //BufferedReader br=new BufferedReader(new FileReader("student.out"));
      try{
         Scanner sc=new Scanner(new FileReader("student.in"));
      //建立一个student.in文件,将学生信息写入其中
         String str;
         for(int i=0;i<5;i++){
             str=sc.next();
         }
         String[] str1=new String[5];
         for(int j=0;j<MAX;j++){
             for(int i=0;i<5;i++){
                str1[i]=sc.next();
             }
             stu[j]=new Student(str1[0],str1[1],str1[2],str1[3],str1[4]);
             Student.count++;
         }
      }
      catch(Exception e){
         System.out.println("The end!");
      }
    }

   public static void InitSystem()throws IOException{
         
         System.out.println("★学生管理系统v1.0★");
         System.out.println("-----------------------");         
         System.out.println("[0].退出程序.");
         System.out.println("[1].输入学生信息,数据以空格隔开,键入end结束输入.");
         System.out.println("[2].加入一个学生信息.");
         Sys