public class Xueyuan { private String name; private String sex ; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Xueyuan(String name,String sex) { super(); this.name = name; this.sex = sex; } }
import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class XueyuanTest { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { Xueyuan stu = new Xueyuan("李明","男"); Xueyuan stu2 = new Xueyuan("张三","男"); Xueyuan stu3 = new Xueyuan("李娜","女"); Map map = new HashMap(); map.put("JACK",stu); map.put("ZED",stu2); map.put("ROSE",stu3); System.out.println("请输入学员英文名:"); String eName = sc.next(); if(map.containsKey(eName)) { Xueyuan xue = (Xueyuan)map.get(eName); System.out.println("英文名为"+eName+"的学员名字:"+xue.getName()+",性别是:"+xue.getSex()); }else { System.out.println("没有找到该学员信息"); } } }