首页 > 其他分享 >7-33 学生列表2 (20 分)

7-33 学生列表2 (20 分)

时间:2022-12-20 17:32:17浏览次数:39  
标签:20 name no 33 cin 列表 int score Student


7-33 学生列表2 (20 分)

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

输入格式:

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

输出格式:

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

输入样例:

在这里给出一组输入。例如:

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

输出样例:

在这里给出相应的输出。例如:

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

参考答案

import java.util.*;

class Student implements Comparable{
private int no;
private String name;
private int score;

public Student(int no, String name, int score) {
this.no = no;
this.name = name;
this.score = score;
}

public Student(int no) {
this.no = no;
}

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

public int getNo() {
return no;
}

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

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return no == student.no;
}

@Override
public int hashCode() {
return Objects.hash(no);
}

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

@Override
public int compareTo(Object o) {
return this.no - ((Student)o).getNo();
}
}

class Main{
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
TreeSet ts = new TreeSet();

for( int i = 0; i < n; i ++ ){
ts.add(new Student(cin.nextInt(), cin.next(), cin.nextInt()));
}
n = cin.nextInt();
for( int i = 0;i < n; i ++ ){
String str_c = cin.next();
switch ( str_c ){
case "add":
ts.add(new Student(cin.nextInt(), cin.next(), cin.nextInt()));
break;
case "delete":
ts.remove(new Student(cin.nextInt()));
break;
case "set":
int no = cin.nextInt();
int score = cin.nextInt();
Iterator it = ts.iterator();
while( it.hasNext() ){
Student s = (Student) it.next();
if( s.getNo() == no ){
s.setScore(score);
}
}
break;
}
}
for( Object o : ts ){
System.out.println(o);
}

cin.close();
}
}


标签:20,name,no,33,cin,列表,int,score,Student
From: https://blog.51cto.com/u_15918664/5956367

相关文章

  • 2019 TRIUMPH ROCKET III ROADSTER SERVICE LAMP RESET
    Background:A2019TRIUMPHROCKETIIIROADSTER,itsmaintenancetimeisup,thedashboardshowsawrenchformaintenancetips.PreparationPreparation: OBDS......
  • ubuntu20.04下C++调用接口解压缩zip文件
     https://github.com/Lecrapouille/zipper Compiling/InstallingTodownloadtheprojectandcompileit:gitclonehttps://github.com/lecrapouille/zipper.g......
  • 咨询-2022-12-20
    咨询发送方packageChat;importjava.io.BufferedReader;importjava.io.InputStreamReader;importjava.net.DatagramPacket;importjava.net.DatagramSocket;importj......
  • 论文解读丨【CVPR 2022】不使用人工标注提升文字识别器性能
    摘要:本文提出了一种针对文字识别的半监督方法。区别于常见的半监督方法,本文的针对文字识别这类序列识别问题做出了特定的设计。本文分享自华为云社区《[CVPR2022]不使用......
  • 2021博客总结
    这不又是快跨年了,新的一年就要到来了,那就辞旧迎新吧,先来总结下2021博客日志回望2021我最早是在2020年开始写博客的这也算我的博客元年了,当时就是把自己平时的笔记发到了c......
  • SQL Server 2019的触发器-trigger
    SQLServer2019的触发器-trigger一、trigger的介绍触发器是特殊的存储过程,它在特定的数据库活动发生时自动执行。触发器可以与特定表上的INSERT、DELETE、UPDATE......
  • 数据传输 | DTLE 3.20.08.0 来啦!
    开源数据传输组件DTLE3.20.08.0已于今日发布。#一、DTLE项目介绍爱可生开源社区的DTLE,自开源起一直定位于一款针对MySQL使用特点、支持多种使用场景的数据传输组件,......
  • Microsoft SQL Server 2008 R2 安装程序出错实例
    出现以下错误:''isnotavalidloginoryoudonothavepermission. 2-报错原因装这么多数据库,第一次遇到这种问题,于是开始上网查,报错原因是这样的:设置的电脑名......
  • 2022.12.20 线段树复习笔记(未完待续)
    线段树原理及存储:如图,1即为根节点,存储着[1,5]的整个区间和,‘1’为左边界,‘5’为右边界,所以此节点表示的是[1,5]这个区间。线段树的每个节点向下二分,左儿子的编号为此节......
  • 我的2022年-总结、感悟、碎碎念
    我的2022年-总结、感悟、碎碎念 又到年底了,总结下2022吧,今年还是蛮多收获和感悟的,感觉越发活的通透了些,有些事情我们无法把握,有些事情我们能把握。淡然面对无法把握的......