Java 筛选优秀学生:成绩大于等于90分且小于等于100分
Java 筛选优秀学生:成绩大于等于90分且小于等于100分
本题要求实现一个 Java 函数,用于筛选出一个班级中的优秀学生(成绩大于等于90分并且小于等于100分)并返回。函数接收一个包含学生信息的列表作为参数,并返回一个包含所有优秀学生的列表,保持原有顺序。
函数接口定义:
函数的原型如下:
public static List<Student> filter(List<Student> students);
其中 students 是用户传入的参数,类 Student 的定义如下:
class Student {
public String name;
public String id;
public double score;
}
要求:
- 函数返回一个
List,其中包含所有优秀(score项不小于 90,但不能超过 100)学生,要求顺序和students相同。
裁判测试程序样例:
我们将使用如下函数检验你的函数:
import java.util.*;
public class Main {
public static class Student {
public String name;
public String id;
public double score;
}
/* 此区间是要编写的函数 */
public static List<Student> filter(List<Student> students) {
// 请补充完整
}
/* 此区间是要编写的函数 */
public static void main(String[] args) {
Scanner key = new Scanner(System.in);
int n = key.nextInt();
List<Student> list = /*接收数据输入,此部分不可见*/;
List<Student> result = filter(list);
for (Student s : result) {
System.out.printf('%s %s %.1f
', s.id, s.name, s.score);
}
}
}
输入样例:
3
XiaoMing 10001 95.5
LiHua 10002 89
WangGang 10003 99.5
输出样例:
XiaoMing 10001 95.5
WangGang 10003 99.5
代码长度限制:
16 KB
时间限制:
400 ms
内存限制内容:
1 MB
解题思路:
遍历输入的学生列表,将成绩大于等于 90 分且小于等于 100 分的学生加入结果列表中,最后返回结果列表。
Java 代码:
import java.util.*;
public class Main {
public static class Student {
public String name;
public String id;
public double score;
}
public static List<Student> filter(List<Student> students) {
List<Student> result = new ArrayList<>();
for (Student student : students) {
if (student.score >= 90 && student.score <= 100) {
result.add(student);
}
}
return result;
}
public static void main(String[] args) {
Scanner key = new Scanner(System.in);
int n = key.nextInt();
List<Student> list = /*接收数据输入,此部分不可见*/;
List<Student> result = filter(list);
for (Student s : result) {
System.out.printf('%s %s %.1f
', s.id, s.name, s.score);
}
}
}
AI 优化版
原文地址:https://www.cveoy.top/t/topic/m0MF 著作权归作者所有。请勿转载和采集!