查找学员
package cn.home; public class Student { private String name; private String gender; public Student() {} public Student(String name, String gender) { super(); this.name = name; this.gender = gender; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } }
package cn.home; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Scanner; import java.util.Set; public class Test { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { Map<String, Student> students = new HashMap<String, Student>(); students.put("Jack",new Student("李明","男") ); students.put("Mike",new Student("张三","男") ); students.put("Rose",new Student("小红","女")); students.put("John",new Student("王五","男")); // Set<String> keys = students.keySet(); // Iterator<String> itor = keys.iterator(); // while(itor.hasNext()) { // // String s1 = itor.next(); // Student value = students.get(s1); // System.out.println(s1+"的学员姓名是:"+value.getName()+",性别是"+value.getGender()); // } System.out.println("请输入要查找的英文名:"); String xname = sc.next(); if(students.containsKey(xname)) { Student stu = students.get(xname); System.out.println("你要找的是"+stu.getName()+","+stu.getGender()); }else { System.out.println("查无此人"); } } }