|--需求说明
|--实现思路
1、创建学员类,用于初始化信息
2、采用map储存数据,key储存学员的英文名,value储存学员的其他信息
3、通过判断map中有没有用户输入的英文名来判断是否给出学员信息
|--代码内容
1 package kehou; 2 3 /** 4 * @auther::9527 5 * @Description: 学生类 6 * @program: gaoJiTeXing 7 * @create: 2019-07-27 10:36 8 */ 9 public class Student { 10 private String ename; //英文名 11 private String cname; //中文名 12 private int id; //员工工号 13 private String gender; //性别 14 15 public Student() { 16 } 17 18 public Student(String ename, String cname, int id, String gender) { 19 this.ename = ename; 20 this.cname = cname; 21 this.id = id; 22 this.gender = gender; 23 } 24 25 public String getEname() { 26 return ename; 27 } 28 29 public void setEname(String ename) { 30 this.ename = ename; 31 } 32 33 public String getCname() { 34 return cname; 35 } 36 37 public void setCname(String cname) { 38 this.cname = cname; 39 } 40 41 public int getId() { 42 return id; 43 } 44 45 public void setId(int id) { 46 this.id = id; 47 } 48 49 public String getGender() { 50 return gender; 51 } 52 53 public void setGender(String gender) { 54 this.gender = gender; 55 } 56 }
1 package kehou; 2 3 import java.util.HashMap; 4 import java.util.Scanner; 5 6 /** 7 * @auther::9527 8 * @Description: 程序入口 9 * @program: gaoJiTeXing 10 * @create: 2019-07-27 10:45 11 */ 12 public class StudentTest { 13 static Scanner scanner = new Scanner(System.in); 14 public static void main(String[] args) { 15 //String eName, String cName, int id 16 Student st1 = new Student("Jack","李霖",58,"男"); 17 Student st2 = new Student("Jone","郑书豪",25,"男"); 18 Student st3 = new Student("Tom","黎明",32,"女"); 19 Student st4 = new Student("Evan","李明丽",8,"女"); 20 21 //创建map集合 22 HashMap<String,Student> map = new HashMap<>(); 23 map.put(st1.getEname(),st1); 24 map.put(st2.getEname(),st2); 25 map.put(st3.getEname(),st3); 26 map.put(st4.getEname(),st4); 27 28 //接受用户信息 29 System.out.println("请输入学员英文名"); 30 String ename = scanner.next(); 31 32 //根据用户输入的信息进行查找 33 if (map.containsKey(ename)){ 34 System.out.println("姓名是:"+map.get(ename).getCname()+"\t性别是:"+map.get(ename).getGender()); 35 }else { 36 System.out.println("没有你要找的人"); 37 } 38 } 39 }
|--运行结果