当前位置: 代码迷 >> Java Web开发 >> ★关于排序的有关问题★
  详细解决方案

★关于排序的有关问题★

热度:309   发布时间:2016-04-17 12:32:37.0
★★关于排序的问题★★
这是我编的一个学生成绩管理系统 
我想用根据总分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].加入一个学生信息.");         System.out.println("[3].删除一个学生信息.");         System.out.println("[4].按学号查找学生及成绩.");                 System.out.println("[5].按总分数排序.");         System.out.println("[6].查看分数,平均分及等级.");         String str;         System.out.println("-----------------------");            System.out.print("输入你的选择:");         BufferedReader br=new BufferedReader(new InputStreamReader(System.in));         str=br.readLine();         int i=Integer.parseInt(str);         switch(i){           case 1:               InputStuInfo();               InitSystem();               break;           case 2:               AddStuInfo();               InitSystem();               break;           case 3:                                InitSystem();                break;           case 4:                SearchInfo();                InitSystem();                break;                      case 5:                                InitSystem();                break;                                       case 6:                Display();                InitSystem();                break;           case 0:                System.exit(1);                break;           default:                 InitSystem();                 break;        }   }   public static void InputStuInfo() throws IOException{         String[] str=new String[5];         String str1;         for(int i=0;Student.count!=0;i++){            Student.count--;            }         System.out.println("id\tname\tage\tscore1\tscore2");         //Student[] stu=new Student[6];         outer:         for(int j=0;j<MAX;j++){            Scanner sc=new Scanner(System.in);            for(int i=0;i<5;i++){                str[i]=sc.next();                if(str[i].equals("end"))  //输入end结束循环                    break outer;            }            stu[j]=new Student(str[0],str[1],str[2],str[3],str[4]);            Student.count++;         }         Output();   }   public static void AddStuInfo()throws IOException{       String[] str=new String[5];       String str1;       Scanner sc=new Scanner(System.in);       System.out.println("id\tname\tage\tscore1\tscore2");       for(int i=0;i<5;i++)                str[i]=sc.next();       if(Student.count==MAX){           System.out.println("无法添加学生,系统已满");       }       else{            stu[Student.count]=new Student(str[0],str[1],str[2],str[3],str[4]);            Student.count++;       }       Output();   }      public static void SearchInfo()throws IOException{      System.out.print("请输入需要查找的学生的学号:");       BufferedReader br=new BufferedReader(new InputStreamReader(System.in));      String str=br.readLine();      int i;      i=Search(str);      if(i==-1)          System.out.println("不能找到相关学生信息"+str);      else{System.out.println("要查找的学生为:"+stu[i].GetId()+"\t"+stu[i].GetName()+"\t"+stu[i].GetAge()+"\t"+stu[i].GetScore1()+"\t"+stu[i].GetScore2()+"\t"+stu[i].GetTotal());}     }   public static void Display(){       System.out.println("id\tname\tage\tscore1\tscore2\ttotal\tave\tlevel");       for(int i=0;i<Student.count;i++){           stu[i].Display();       }   }   public static int Search(String str){       int i;       for(i=0;i<MAX;i++){          if(str.equals(stu[i].GetId()))             break;       }       if(i==MAX)          return -1;       else          return i;   }   public static void main(String[] args)throws IOException{          InitSystem();          Input();    }}
  相关解决方案