public void showStudents() { for (Student student : students) { System.out.println("姓名:" + student.getName()); System.out.println("年龄:" + student.getAge()); System.out.println("学号:" + student.getId()); System.out.println("专业:" + student.getMajor()); System.out.println(); }什么意思
时间: 2024-03-18 19:44:11 浏览: 72
这段代码是一个Java类中的方法,用于展示一个学生(Student)对象的信息。方法名为"showStudents",没有返回值,使用了一个for循环遍历了一个名为"students"的学生对象数组。在循环中,使用了Student类中的四个方法分别获取学生的姓名、年龄、学号和专业,并打印在屏幕上。每个学生的信息之间用空行隔开,以便于区分。该方法没有访问修饰符,意味着只能在类的内部调用,无法从类的外部访问。
相关问题
import java.util.Arrays; public class Task { public static void main(String[] args) { Student[] students = { new Student("王五", 1.6, 20), new Student("李四", 1.7, 19), new Student("赵六", 1.7, 20), new Student("tianqi", 1.8, 22), new Student("tianan", 1.8, 22), new Student("张三", 1.8, 18), new Student("王振", 1.6, 22), }; sortStudent(students); System.out.println("---------------------"); sortStudentByAgeDesc(students); } public static void sortStudentByAgeDesc(Student[] students) { // 请通过MyCompare的compareByAgeDesc方法实现按照年龄降序排序 /* TODO */ for (Student student : students) { System.out.println(student.getName() + ": " + student.getAge() + " -> " + student.getHeight()); } } public static void sortStudent(Student[] students) { // 请通过MyCompare类的compareStudent方法 // 实现按照年龄升序排序、再按照身高降序排序 /* TODO */ for (Student student : students) { System.out.println(student.getName() + ": " + student.getAge() + " -> " + student.getHeight()); } } }Lambda表达式:方法引用 任务 请在sortStudent方法中: 通过MyCompare类的compareStudent方法,以Lambda表达式的形式实现按照年龄升序排序、再按照身高降序排序 请在sortStudentByAgeDesc方法中 通过MyCompare的compareByAgeDesc方法实现按照年龄降序排序
可以使用Lambda表达式来实现按照年龄升序排序、再按照身高降序排序,代码如下:
public static void sortStudent(Student[] students) {
// 请通过MyCompare类的compareStudent方法
// 实现按照年龄升序排序、再按照身高降序排序
Arrays.sort(students, (s1, s2) -> {
if (s1.getAge() != s2.getAge()) {
return s1.getAge() - s2.getAge();
} else {
return Double.compare(s2.getHeight(), s1.getHeight());
}
});
for (Student student : students) {
System.out.println(student.getName() + ": " +
student.getAge() + " -> " +
student.getHeight());
}
}
在sortStudentByAgeDesc方法中,可以通过MyCompare的compareByAgeDesc方法实现按照年龄降序排序,代码如下:
public static void sortStudentByAgeDesc(Student[] students) {
// 请通过MyCompare的compareByAgeDesc方法实现按照年龄降序排序
Arrays.sort(students, MyCompare::compareByAgeDesc);
for (Student student : students) {
System.out.println(student.getName() + ": " +
student.getAge() + " -> " +
student.getHeight());
}
}
注意:在使用Lambda表达式时,需要使用Arrays.sort方法来进行排序,而不是自己实现排序算法。
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class Student { private String name; private int age; private int chinesescore; private int mathsscore; private int englishscore; public Student(String name, int age, int chinesescore, int mathsscore, int englishscore) { this.name = name; this.age = age; this.chinesescore = chinesescore; this.mathsscore = mathsscore; this.englishscore = englishscore; } public String getName() { return name; } public int getAge() { return age; } public int getch() { return chinesescore; } public int getma() { return mathsscore; } public int geteng() { return englishscore; } public int gettotal() { return englishscore + chinesescore + mathsscore; } public void printScores() { System.out.println(getName()); System.out.println(getAge()); System.out.println(getch()); System.out.println(getma()); System.out.println(geteng()); } public static void main(String[] args) { Student student1 = new Student("张三", 18, 80, 90, 85); Student student2 = new Student("李四", 17, 75, 95, 90); Student student3 = new Student("王五", 16, 85, 80, 95); student1.printScores(); student2.printScores(); student3.printScores(); } List<Student> students = new ArrayList<>(); Comparator<Student> comparator = Comparator.comparing(Student::gettotal) .reversed() .thenComparing(Student::getch, Comparator.reverseOrder()) .thenComparing(Student::getma, Comparator.reverseOrder()) .thenComparing(Student::getAge); Collections.sort(student, comparator); for (Student student : students) { System.out.println(student); } }
这段代码是一个学生类(Student),其中包含学生的姓名(name)、年龄(age)以及语文成绩(chinesescore)、数学成绩(mathsscore)、英语成绩(englishscore)。代码中定义了一个构造函数来初始化学生对象,还有一些用于获取成员变量的方法。在main方法中,创建了三个学生对象并调用了printScores方法来打印学生的成绩。最后,定义了一个students列表来存储学生对象,并使用Comparator对学生对象进行排序,排序规则是先按照总分从高到低排列,如果总分相同则按照语文成绩、数学成绩和年龄进行降序排列。然后通过循环遍历打印排序后的学生对象。
不过我发现代码中有一个错误:在Collections.sort方法中,应该传入的是students列表而不是student。请将代码中的student改为students,如下所示:
```java
Collections.sort(students, comparator);
for (Student student : students) {
System.out.println(student);
}
```
这样就可以正确地对学生对象进行排序并打印出来了。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)