用java创建一个学生类,属性包括:学号、姓名,和Java、C语言、数据结构三门课程成绩 //使用ArrayList<Student>保存10个学生对象, //分别对这些学生按照Java、C语言、数据结构和总成绩进行排序 //lamda //Comparable compare(Object o1,Object o2)
时间: 2023-12-03 19:02:28 浏览: 91
设计学生类Student,属性:编号(整型);姓名(字符串),成绩(整型)。
5星 · 资源好评率100%
好的,我可以帮您回答这个问题。
首先,我们可以创建一个名为Student的类,包括学号、姓名和三门课程成绩:
```java
public class Student {
private int id;
private String name;
private int javaScore;
private int cScore;
private int dataStructureScore;
public Student(int id, String name, int javaScore, int cScore, int dataStructureScore) {
this.id = id;
this.name = name;
this.javaScore = javaScore;
this.cScore = cScore;
this.dataStructureScore = dataStructureScore;
}
// getter and setter methods
}
```
接下来,我们可以使用ArrayList来保存10个学生对象,并使用lamda表达式对它们进行排序:
```java
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Student> students = new ArrayList<>();
students.add(new Student(1, "张三", 80, 90, 70));
students.add(new Student(2, "李四", 90, 60, 80));
students.add(new Student(3, "王五", 70, 80, 90));
students.add(new Student(4, "赵六", 60, 70, 80));
students.add(new Student(5, "钱七", 80, 60, 70));
students.add(new Student(6, "孙八", 90, 80, 60));
students.add(new Student(7, "周九", 70, 60, 80));
students.add(new Student(8, "吴十", 60, 80, 70));
students.add(new Student(9, "郑一", 80, 70, 60));
students.add(new Student(10, "王二", 70, 80, 70));
// 按Java成绩排序
students.sort((s1, s2) -> s2.getJavaScore() - s1.getJavaScore());
System.out.println("按Java成绩排序:");
students.forEach(System.out::println);
// 按C语言成绩排序
students.sort(Comparator.comparingInt(Student::getCScore).reversed());
System.out.println("按C语言成绩排序:");
students.forEach(System.out::println);
// 按数据结构成绩排序
students.sort(Comparator.comparingInt(Student::getDataStructureScore).reversed());
System.out.println("按数据结构成绩排序:");
students.forEach(System.out::println);
// 按总成绩排序
students.sort((s1, s2) -> {
int totalScore1 = s1.getJavaScore() + s1.getCScore() + s1.getDataStructureScore();
int totalScore2 = s2.getJavaScore() + s2.getCScore() + s2.getDataStructureScore();
return totalScore2 - totalScore1;
});
System.out.println("按总成绩排序:");
students.forEach(System.out::println);
}
}
```
最后,如果您想使用Comparable接口来实现排序,可以在Student类中实现该接口并重写compareTo方法:
```java
public class Student implements Comparable<Student> {
// ...
@Override
public int compareTo(Student o) {
int totalScore1 = this.getJavaScore() + this.getCScore() + this.getDataStructureScore();
int totalScore2 = o.getJavaScore() + o.getCScore() + o.getDataStructureScore();
return totalScore2 - totalScore1;
}
}
```
然后,您可以使用Collections.sort方法来对学生列表进行排序:
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Student> students = new ArrayList<>();
students.add(new Student(1, "张三", 80, 90, 70));
students.add(new Student(2, "李四", 90, 60, 80));
students.add(new Student(3, "王五", 70, 80, 90));
students.add(new Student(4, "赵六", 60, 70, 80));
students.add(new Student(5, "钱七", 80, 60, 70));
students.add(new Student(6, "孙八", 90, 80, 60));
students.add(new Student(7, "周九", 70, 60, 80));
students.add(new Student(8, "吴十", 60, 80, 70));
students.add(new Student(9, "郑一", 80, 70, 60));
students.add(new Student(10, "王二", 70, 80, 70));
Collections.sort(students);
System.out.println("按总成绩排序:");
students.forEach(System.out::println);
}
}
```
希望这可以帮助到您!
阅读全文