学生信息的添加与查询
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
设计一个学生信息添加和查询的系统,从键盘读入学生的数据,然后通过屏幕进行显示。
Input
第一行有1个整数N,表示学生数量;
接下来有N行学生数据,分别表示学生的id(编号)、name(姓名)、birthday(生日)、score(成绩)属性的值,关键字(id)相同的记录代表同一个学生(如果id相同,后来读入的学生信息会覆盖已有的学生信息)
Output
按照id从小到大的顺序,输出所有学生的属性名称及属性值,其中score(成绩)保留1位有效数字,具体输出格式见输出样例,属性之间用“\t”进行分隔。
Sample Input
5 0001 Mike 1990-05-20 98.5 0002 John 1992-05-20 67 0003 Hill 1994-05-20 36.5 0004 Christ 1996-05-02 86.5 0001 Jack 1998-05-20 96
Sample Output
id:0001 name:Jack birthday:1998_5_20 score:96.0 id:0002 name:John birthday:1992_5_20 score:67.0 id:0003 name:Hill birthday:1994_5_20 score:36.5 id:0004 name:Christ birthday:1996_5_2 score:86.5
Hint
Source
zhouxq
设计知识总结:
map.keySet()
该方法返回map中所有key值的列表。
详细看: https://blog.csdn.net/qq_30507287/article/details/80488512
关于重写 toString 里的catch 异常没搞懂 搞懂再补充:
Collections.sort() 重载 等搞懂补充
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
Map<String, Student> map = new HashMap<String, Student>();
for (int i = 1; i <= n; i++) {
String id = in.next();
Student s = new Student(in.next(), in.next(), in.nextDouble());
map.put(id, s);
}
Set<String> keySet = map.keySet();
List<String> list = new ArrayList<String>(keySet);
Collections.sort(list, new Comparator<String>() { // 暂时没搞懂为什么要排序
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
});
for (int i = 0; i < list.size(); i++) {
System.out.print("id:" + list.get(i) + "\t");
System.out.println(map.get(list.get(i))); // 会自动调用toString方法
}
}
}
class Student {
private String name;
private String BirthDate;
private double score;
public Student(String name, String BirthDate, double score) {
this.name = name;
this.BirthDate = BirthDate;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBirthDate() {
return BirthDate;
}
public void setBirthDate(String BirthDate) {
this.BirthDate = BirthDate;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public String toString() { // 重写toString
try {
return "name:" + name + "\tbirthday:" + DateUtil.DateConvert(BirthDate) + "\tscore:" + String.format("%.1f",score);
} catch (ParseException e) {
e.printStackTrace(); // ?? 没搞懂干嘛的
}
return BirthDate;
}
}
class DateUtil {
public static String DateConvert(String BirthDate) throws ParseException { // 不加后面那句异常 暂时没搞懂
SimpleDateFormat s1 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat s2 = new SimpleDateFormat("yyyy_M_d");
String birth = s2.format(s1.parse(BirthDate));
return birth;
}
}