package testmap; import com.sun.applet2.AppletParameters; import java.util.*; public class TestHashMap7 { public static void main(String[] args) { /*定义学生类:属性为学号-num、姓名、年龄、成绩 根据操作选项完成以下操作:输入以下数据完成对应的操作 * 1. 利用 HashMap存储学生信息,用学号作为-key;学生信息作为-value * 2. 打印输出集合中所有的学生信息 3. 输入一个学生,查询对应的学生信息 4. 输入一个成绩score,打印输大于该成绩的所有信息 * */ studentProject project = new studentProject(); project.menue(); } } class studentProject{ private HashMap<Integer,student> map = new HashMap<>(); private Scanner sc = new Scanner(System.in); public void menue(){ int select ; do{ System.out.println("欢迎进入简易学生系统管理,您可以选择的操作如下:"); System.out.println("1.添加学生(添加一个,能力允许的同学扩充:可以添加多个)"); System.out.println("2.展示所有学生信息 3.输入学生姓名,展示学生信息"); System.out.println("4.输入学号,展示对应的学生信息"); System.out.println("5.输入成绩score,展示大于该成绩的学生信息"); System.out.println("6.将map中的学生信息进行永久性存储(利用IO写入到文件中,格式 100/赵丽颖/18/99.0)-选做"); System.out.println("7.退出系统"); System.out.println("欢迎进入简易学生系统管理,您可以选择的操作如下:"); select = sc.nextInt(); switch (select){ case 1: addstudent();break; case 2: showAll();break; case 3: selectByName();break; case 4: selectByNum();break; case 5: selectByScore();break; case 6: saveStudentToFile();break; case 7: System.out.println("退出系统,欢迎下次使用!!!");break; default: System.out.println("输入不合法,请重新输入"); } }while (select!=7); } private void addstudent(){ System.out.println("添加学生信息:"); System.out.println("输入学号:"); Integer num = sc.nextInt(); System.out.println("输入姓名:"); String name= sc.next(); System.out.println("输入年龄:"); int age = sc.nextInt(); System.out.println("输入成绩:"); Double score = sc.nextDouble(); student stu = new student(num,name,age,score); map.put(num,stu); } private void showAll(){ Set<Integer> ks= map.keySet(); for(Integer s:ks){ student stu=map.get(s); System.out.println(stu); } } private void selectByName(){ System.out.println("请输入学生姓名:"); String name = sc.next(); Collection<student> cs = map.values(); for (student s : cs) { if (name.equals(s.getName())) { System.out.println(s); } } } private void selectByNum(){ System.out.println("请输入学生学号:"); Integer num = sc.nextInt(); if (map.containsKey(num)) { student s = map.get(num); System.out.println(s); } else { System.out.println("未找到该学号的学生!"); } } private void selectByScore(){ System.out.println("请输入成绩:"); Double score = sc.nextDouble(); for (student s : map.values()) { if (score> s.getScore()) { System.out.println(s); } } } private void saveStudentToFile(){ System.out.println("将Map中的学生信息永久性储存"); } } // 学生类根据要求完成,并封装 class student{ private Integer num; private String name; private int age; private Double score; public student() { } public student(Integer num, String name, int age, Double score) { this.num = num; this.name = name; this.age = age; this.score = score; } public Integer getNum() { return num; } public void setNum(Integer num) { this.num = num; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Double getScore() { return score; } public void setScore(Double score) { this.score = score; } }