7-3 学生列表 (20 分)

该程序实现了对学生信息的管理,包括添加、删除和修改记录。通过Scanner读取用户输入,根据操作类型动态更新存储在LinkedList中的Student对象。每个Student对象包含姓名、学号和分数属性,并提供了相应的getter和setter方法。程序最后输出所有学生信息。
摘要由CSDN通过智能技术生成

在这里插入图片描述
在这里插入图片描述

import javax.swing.*;
import java.lang.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.Date;
import java.util.Scanner;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Comparator;

class Student {
    String name;
    int no;
    int score;

    public Student(int no_,String name_,int score_){
        no=no_;
        name=name_;
        score=score_;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public int getNo() {
        return no;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public int getScore() {
        return score;
    }

    @Override
    public int hashCode(){
        final int prime = 31;
        int result = 1;
        result = prime * result + no;
        return result;
    }

    @Override
    public boolean equals(Object o1) {
        if (o1 == null)
            return false;
        if(o1 instanceof Student){
            Student s1 = (Student) o1;
            if(this.no == s1.no && this.score == s1.score && this.name.equals(s1.name))
                {
                    return true;
                }
        }
        return false;
    }

    @Override
    public String toString(){
        return "no:"+no +" name:"+name +" score:"+score;
    }
}

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n=sc.nextInt();
        List<Student> studentList=new LinkedList<Student>() ;

        for(int i=0;i<n;i++){
            int no=sc.nextInt();
            String na=sc.next();
            int sco=sc.nextInt();
            Student st =new Student(no,na,sco);
            studentList.add(st);
        }
        int num_ope = sc.nextInt();
        for(int i=0;i<num_ope;i++){
            String ope=sc.next();
            if(ope.charAt(0)=='a'){
                Student stu_add = new Student(sc.nextInt(), sc.next(),sc.nextInt());
                studentList.add(stu_add);
            }
            else if(ope.charAt(0)=='d'){
                int no_del=sc.nextInt();
                for(int j=0;j<studentList.size();j++){
                    if(studentList.get(j).getNo()==no_del){
                        studentList.remove(j);
                        j--;
                    }
                }
            }
            else {
                int no_set=sc.nextInt();
                int score_set=sc.nextInt();
                for(int j=0;j<studentList.size();j++){
                    if(studentList.get(j).getNo()==no_set){
                        studentList.get(j).setScore(score_set);
                    }
                }

            }
        }
        for(int i=0;i<studentList.size();i++){
            System.out.println(studentList.get(i).toString());
        }

    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值