学生成绩的排名

package com.paixu;

public class Student {
String name;
int score;
public Student(String name, int score) {
    super();
    this.name = name;
    this.score = score;
}

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public int getScore() {
    return score;
}
public void setScore(int score) {
    this.score = score;
}
    
    
}
package com.paixu;

public class Compute {
  Student [] stu;
  int size;
  
public Compute(int size){
    stu =new Student[size];
}
public Compute(){
    this(10);
}
  
  public void add(Student s){
      stu[size++]=s;
  }
  
  public void display(){
      for(int i=0;i<=size;i++){
          System.out.println(stu[i].getName()+":"+stu[i].getScore());
      }
  }

  public void sort(){
      for(int i=0;i<size-1;i++){
          for(int j=0;j<size-1-i;j++){
              if(stu[j].getScore()<stu[j+1].getScore()){
                  Student s=stu[j];
                  stu[j]=stu[j+1];
                  stu[j+1]=s;
              }
          }
      }
  }
  
  public void pass(){
      sort();
    int s=size*7/10;  
      for(int i=0;i<s;i++){
          System.out.println(stu[i].getName()+":"+stu[i].getScore());
      }
  }
    
}
package com.paixu;
/*
 * 8.编写一个程序,统计学生成绩,其功能包括输入学生的姓名和成绩,
 * 按成绩从高到低排列打印输出,对前%70的学生定为合格(PASS),
 * 而后30%的学生定为不合格(FAIL)
思路:
    设计一个类student,包含学生的姓名和成绩等数据。
    设计一个类Compute,这个类中包含了多个学生的信息,方法有 sort()、
    disp(),,它们分别用于按成绩排序和输出数据。

 */
public class Test {

    public static void main(String[] args) {
        Compute c=new Compute();
    c.add(new Student("j", 1));
    c.add(new Student("i", 2));
    c.add(new Student("h", 3));
    c.add(new Student("g", 4));
    c.add(new Student("f", 5));
    c.add(new Student("e", 6));
    c.add(new Student("d", 7));
    c.add(new Student("c", 8));
    c.add(new Student("b", 9));
    c.add(new Student("a", 10));
        
c.pass();
    }

}

 

posted @ 2017-04-20 18:04  苏轼的红烧肉  阅读(305)  评论(0编辑  收藏  举报