import java.util.*;import javax.swing.JOptionPane;public class School {
HashMap map; public School() {
map = new HashMap();
} public void addStudent(int id, Student st) {
map.put(id, st);
} public Object findStudent(int id) {
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry=(Map.Entry)it.next();
if (map.containsKey(id)) {
return ((Student) map.get(id));
}
}
return null;
} public static void main(String[] args) {
School sch = new School();
sch.addStudent( 0,new Student(101, "kobe", 45));
sch.addStudent(0, new Student(121, "xiaoyang", 65));
sch.addStudent(0, new Student(114, "xiaolong", 85));
sch.addStudent(0, new Student(127, "lao", 100));
sch.addStudent(0, new Student(119, "zhuge", 0));
String str = JOptionPane.showInputDialog("请输入要查找的学生的学号:");
int id = Integer.parseInt(str);
Student st = (Student) sch.findStudent(id);
if (st == null) {
System.out.println("你要查找的学生不存在");
} else {
System.out.println("学号" + st.getId());
System.out.println("姓名" + st.getName());
System.out.println("年龄" + st.getScore());
}
}}
public class Student {
private int id;
private String name;
private double score; public Student(int id, String name, double score) {
this.id = id;
this.name = name;
this.score = score;
} public int getId() {
return id;
} public String getName() {
return name;
} public double getScore() {
return score;
}
}

解决方案 »

  1.   

    sch.addStudent( 0,new Student(101, "kobe", 45));
    这个有问题,前面一参数要传入ID
      

  2.   

    sch.addStudent(101,new Student(101, "kobe", 45));
    sch.addStudent(121, new Student(121, "xiaoyang", 65));
    sch.addStudent(114, new Student(114, "xiaolong", 85));
    sch.addStudent(127, new Student(127, "lao", 100));
    sch.addStudent(119, new Student(119, "zhuge", 0));
      

  3.   

    3楼的应该没问题,
    KEY值建议用包装类