58,452
社区成员
发帖
与我相关
我的任务
分享
public class Test3 {
public static void main(String[] args) {
Student[] student = new Student[8];
student[0] = new Student(20160001, "学生一", 85, 72, 95);
student[1] = new Student(20160002, "学生二", 87, 72, 93);
student[2] = new Student(20160003, "学生三", 88, 73, 91);
student[3] = new Student(20160004, "学生四", 99, 94, 56);
student[4] = new Student(20160005, "学生五", 100, 100, 100);
student[5] = new Student(20160006, "学生六", 35, 78, 83);
student[6] = new Student(20160007, "学生七", 87, 66, 78);
student[7] = new Student(20160008, "学生八", 87, 66, 78);
Student temp; //排序使用的临时对象
//按sum排序
for (int i = 0; i < student.length; i++) {
for (int j = i+1; j < student.length; j++) {
if(student[j].sum() > student[i].sum()){
temp = student[i];
student[i] = student[j];
student[j] = temp;
}
}
}
int mingci = 1;//名次
for (int i = 0; i < student.length-1; i++) {
int n = checkContinue(student, student[i].sum());
if (n == 1) {
student[i].mingci = mingci++;
}else {
//总分相同,名次相同
for (int j = 0; j < n; j++) {
student[i+j].mingci = mingci;
}
mingci++;
i = i + n -1;//连续n个相同的总分,排名一样
}
}
student[student.length-1].mingci = mingci;
for (int i = 0; i < student.length; i++) {
System.out.println(student[i]) ;
}
}
//判断是否连续
public static int checkContinue(Student[] student,double sum){
int count = 0 ;//统计多少个连续相同的sum
for (int i = 0; i < student.length; i++) {
if(student[i].sum() == sum){
count++;
}
}
return count;
}
}
class Student {
public String name;
public int xuehao, mingci;
public double a1, a2, a3;
Student(int xuehao, String name, double a1, double a2, double a3) {
this.xuehao = xuehao;
this.name = name;
this.a1 = a1;
this.a2 = a2;
this.a3 = a3;
}
double sum() {
return (this.a1 + this.a2 + this.a3);
}
public String toString(){
return "名次:" + this.mingci + " 学号:" + this.xuehao+" "+this.name+" "+this.a1+" "+
this.a2+" "+this.a3+" 总分:"+this.sum();
}
}
名次:1 学号:20160005 学生五 100.0 100.0 100.0 总分:300.0
名次:2 学号:20160002 学生二 87.0 72.0 93.0 总分:252.0
名次:2 学号:20160003 学生三 88.0 73.0 91.0 总分:252.0
名次:2 学号:20160001 学生一 85.0 72.0 95.0 总分:252.0
名次:3 学号:20160004 学生四 99.0 94.0 56.0 总分:249.0
名次:4 学号:20160007 学生七 87.0 66.0 78.0 总分:231.0
名次:4 学号:20160008 学生八 87.0 66.0 78.0 总分:231.0
名次:5 学号:20160006 学生六 35.0 78.0 83.0 总分:196.0
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Student implements Comparable<Student>{
public String name;
public int xuehao, mingci;
public double a1, a2, a3;
public Student(int xuehao, String name, double a1, double a2, double a3) {
this.xuehao = xuehao;
this.name = name;
this.a1 = a1;
this.a2 = a2;
this.a3 = a3;
}
public double sum() {
return (this.a1 + this.a2 + this.a3);
}
@Override
public int compareTo(Student o) {
double o1Sum = this.sum();
double o2Sum = o.sum();
return o1Sum == o2Sum ? 0 : o1Sum > o2Sum ? 1 : -1;
}
public static void main(String[] args) {
List<Student> students = new ArrayList<Student>();
students.add(new Student(20160001,"学生一",85,72,95));
students.add(new Student(20160002,"学生二",87,88,92));
students.add(new Student(20160003,"学生三",20,60,56));
students.add(new Student(20160004,"学生四",99,94,59));
students.add(new Student(20160005,"学生五",100,100,100));
System.out.println("初始顺序(姓名,总分)");
for (Student p : students) {
System.out.println(p.name + " | " + p.sum());
}
Collections.sort(students);
System.out.println("排序后(姓名,总分,名次)");
int index = 0;
for (int i = 0; i < students.size(); i++) {
Student p = students.get(i);
if(i == 0 || p.sum() > students.get(i-1).sum()){
index ++ ;
}
System.out.println(p.name + " | " + p.sum() + " | " + index);
}
}
}