167 - 学生列表

167 - 学生列表

Time Limit: 1000   Memory Limit: 65535
Submit: 229  Solved: 148

Description

编写学生类,包含学号no、姓名name、成绩score,提供必要的构造函数、toString函数和equals/hashcode函数,其中,toString函数的格式为“no:xxx name:xxx score:xxx”,no参与equals和hashcode的计算
在main函数中构造一个学生列表对象(List),用于存放多个学生对象
从命令行输入多个学生对象,存入列表中
从命令行中读入在列表对象上的操作,具体操作包含:
add 添加一个学生(包含学号和学生姓名)
delete 删除一个学生(包含学号)
set 修改一个学生信息(只修改某学号学生的成绩)
完成操作后按列表顺序输出集合中的学生

Input

学生个数
学生对象数据
操作数
操作内容

Output

列表顺序输出集合中的学生

Sample Input

4
1 wong 90
2 liu 80
3 chen 70
4 fang 60
3
add 5 duan 80
delete 3
set 4 70

Sample Output

no:1 name:wong score:90
no:2 name:liu score:80
no:4 name:fang score:70
no:5 name:duan score:80

HINT

 

Pre Append Code

Post Append Code

 

import java.util.List;
import java.util.LinkedList;

import java.util.Scanner;

class Student {//定义Student类

    

    int no;

    String name;

    int score;

    

    Student(int no,String name,int score)//构造函数

    {

        this.no=no;

        this.name=name;

        this.score=score;

    }

    

    @Override

    public String toString(){//toString函数

        return "no:"+no+" name:"+name+" score:"+score;

    }

    

    public boolean equals(Student s)//equals/hashcode函数!!!

    {

        if(this.no>s.no)

            return true;

        else 

            return false;

    }

    

    }

public class Main {

    

    public static void main (String []args)

    {

        List<Student> list =new LinkedList<Student>();//构造一个列表(List)!!!

        Scanner scan = new Scanner(System.in);//读入

        int num=scan.nextInt();

        int i;

        for(i=0;i<num;i++)//构造一个学生列表对象(List)

        {

            int no=scan.nextInt();

            String name= scan.next();//!!!

            int score=scan.nextInt();

            Student s =new Student(no,name,score);

            list.add(s);

         }

        

        int j=scan.nextInt();

        for(i=0;i<j;i++)

        {

            String flag=scan.next();//!!!

            //add 添加一个学生(包含学号和学生姓名)

            if(flag.equals("add"))//!!!

            {

                int no=scan.nextInt();

                String name= scan.next();

                int score=scan.nextInt();

                Student u =new Student(no,name,score);

                list.add(u);

            }

            //delete 删除一个学生(包含学号)

            else if(flag.equals("delete"))//!!!

            {

                int no=scan.nextInt();

                /*1、最方便的foreach循环遍历, for(实体名 p:list)

                  2、用iterator进行遍历

                Iterator<String> iter = list.iterator();

                while(iter.hasNext()){     

                        iter.next();  

                  }

                  */

                for(Student s : list)

                {

                    if(s.no==no)

                    {

                    list.remove(s);

                    break;

                    }

                }

            }

            //set 修改一个学生信息(只修改某学号学生的成绩)

            else if(flag.equals("set")) {//!!!

                int no=scan.nextInt();

                int score=scan.nextInt();

                for(Student s : list)

                {

                    if(s.no==no)

                    {

                    s.score=score;

                    break;

                    }

                }

                

            }

            

        }

        for(Student s : list)

            System.out.println(s.toString());//!!!

    }

    

    }


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值