本题要求实现一个函数可筛选出一个班级中的优秀学生成绩大于或等于90分并且小于等于100分并返回。 函数接口定义: 函数的原型如下: public static ListStudent filterListStudent students; 其中 students 是用户传入的参数类Student的定义如下: class Student public String name;
Java 代码如下:
import java.util.ArrayList; import java.util.List;
public class Main {
public static List<Student> filter(List<Student> students) {
List<Student> excellentStudents = new ArrayList<>();
for (Student student : students) {
if (student.score >= 90 && student.score <= 100) {
excellentStudents.add(student);
}
}
return excellentStudents;
}
public static void main(String[] args) {
List<Student> students = new ArrayList<>();
students.add(new Student("Tom", "1", 80));
students.add(new Student("Jack", "2", 95));
students.add(new Student("Rose", "3", 100));
students.add(new Student("Mike", "4", 88));
List<Student> excellentStudents = filter(students);
for (Student student : excellentStudents) {
System.out.println(student.name + " " + student.id + " " + student.score);
}
}
}
class Student { public String name; public String id; public double score;
public Student(String name, String id, double score) {
this.name = name;
this.id = id;
this.score = score;
}
}
运行结果: Jack 2 95.0 Rose 3 100.0
原文地址: https://www.cveoy.top/t/topic/buZL 著作权归作者所有。请勿转载和采集!