(1)定义⼀个学⽣类student,包含姓名和分数两个属性:String name;int score; (2)在主类中,定义⼀个学⽣类数组stu[ ],包含⾄少5个学⽣,对每个学⽣的姓名和分数进⾏赋值; (3)对所有学⽣按照分数进⾏排序,并放⼊stu数组中;注意:如何排序,⽅法不定,请同学们思考。 (4)把所有学⽣信息,按照分数从⾼到底打印出来。
时间: 2024-02-27 21:56:52 浏览: 36
好的,我来帮您解决这个问题。以下是Java代码实现:
```java
public class Student {
String name;
int score;
public Student(String name, int score) {
this.name = name;
this.score = score;
}
}
public class Main {
public static void main(String[] args) {
Student[] stu = new Student[5];
stu[0] = new Student("Tom", 80);
stu[1] = new Student("Jerry", 90);
stu[2] = new Student("Alice", 75);
stu[3] = new Student("Bob", 85);
stu[4] = new Student("Charlie", 95);
// 对所有学生按照分数进行排序
for (int i = 0; i < stu.length - 1; i++) {
for (int j = i + 1; j < stu.length; j++) {
if (stu[i].score < stu[j].score) {
Student temp = stu[i];
stu[i] = stu[j];
stu[j] = temp;
}
}
}
// 打印所有学生信息,按照分数从高到低
for (int i = 0; i < stu.length; i++) {
System.out.println(stu[i].name + " " + stu[i].score);
}
}
}
```
输出结果为:
```
Charlie 95
Jerry 90
Bob 85
Tom 80
Alice 75
```
这里采用的是冒泡排序算法,对所有学生按照分数从高到低进行排序。