CSDN专家-sinjack 实现一个学生管理程序,要求如下:
1.每个学生具有学号,姓名,年龄,成绩等信息
2.用户可以通过学号和姓名查询学生信息
3.该程序可以计算所有学生成绩的平均值、最大值、最小值,以及按照 学号或成绩升序和降序打印所有学生的信息;@
4.学生的信息须本地保存(即关闭该程序后,下次使用时还能读取上次 建立的学生信息)
5.不要求实现用户界面,在命令行下运行即可 ,运行结果截图中须包含所有功能以及情况的展示
import java.util.*;
public class Student {
private String name;
private String sno;
private Integer age;
private double score;
public Student(){
}
public void setScore(double score) {
this.score = score;
}
public double getScore() {
return score;
}
public Student(String name, String sno, Integer age) {
this.name = name;
this.sno = sno;
this.age = age;
}
public static void display(List<Student> list){
double sum=0;
double max=list.get(0).getScore(),min=list.get(0).getScore();
for(Student stu:list){
sum+=stu.getScore();
if(max<stu.getScore()){
max=stu.getScore();
}
if(min>stu.getScore()){
min=stu.getScore();
}
}
System.out.println("最高分为:"+max);
System.out.println("最低分为:"+min);
System.out.println("平均分为:"+sum/list.size());
}
public static void show(List<Student> stus){
Collections.sort(stus, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
// 根据属性进行排序
if (o1.getScore()>o2.getScore()) {
return 1;
}
if (o1.getScore()==o2.getScore()) {
return 0;
}
return -1;
}
});
for (Student student: stus) {
System.out.println(student);
}
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", sno='" + sno + '\'' +
", age=" + age +
", score=" + score +
'}';
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入学生人数:");
int n=sc.nextInt();
List<Student> list=new ArrayList<Student>();
for(int i=0;i<n;i++){
System.out.println("请分别输入第"+(i+1)+"个学生的姓名、学号、年龄、成绩:");
String name=sc.next();
String sno=sc.next();
int age=sc.nextInt();
double score=sc.nextDouble();
Student stu=new Student(name, sno, age);
stu.setScore(score);
list.add(stu);
}
display(list);
System.out.println("学生信息如下:");
show(list);
}
}