PTA期末成绩排序java
时间: 2023-11-09 14:50:37 浏览: 194
定义一个名为Student的类,该类包含学号和成绩两个属性。使用Arrays.sort()方法对学生对象数组按成绩进行排序,然后按照样例输出排名。
```java
import java.util.Arrays;
class Student implements Comparable<Student> {
private int num;
private float score;
public Student(int num, float score) {
this.num = num;
this.score = score;
}
public int getNum() {
return num;
}
public float getScore() {
return score;
}
@Override
public int compareTo(Student s) {
return Float.compare(s.score, this.score);
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Student[] students = new Student[n];
for (int i = 0; i < n; i++) {
int num = sc.nextInt();
float score = sc.nextFloat();
students[i] = new Student(num, score);
}
Arrays.sort(students);
int m = 1;
int buff = 1;
for (int i = 0; i < n; i++) {
System.out.printf("%d %d %.2f\n", m, students[i].getNum(), students[i].getScore());
if (i + 1 < n && students[i].getScore() == students[i + 1].getScore()) {
buff++;
} else {
m += buff;
buff = 1;
}
}
}
}
```
阅读全文