【0727作业】根据学院英文名找到学员对象
1 package com.student; 2 3 public class Student { 4 private String name; 5 private char gender; 6 public String getName() { 7 return name; 8 } 9 public void setName(String name) { 10 this.name = name; 11 } 12 public char getGender() { 13 return gender; 14 } 15 public void setGender(char gender) { 16 this.gender = gender; 17 } 18 public Student(String name, char gender) { 19 super(); 20 this.name = name; 21 this.gender = gender; 22 } 23 24 }
测试
1 package com.student; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 import java.util.Scanner; 6 7 public class Test { 8 static Scanner sc=new Scanner(System.in); 9 public static void main(String[] args) { 10 Map<String,Student>student=new HashMap<String, Student>(); 11 student.put("Jack", new Student("李明", '男')); 12 student.put("David", new Student("王卫", '男')); 13 student.put("Chris", new Student("李葱", '女')); 14 student.put("Jinna", new Student("傅青", '女')); 15 16 System.out.println("请输入学员的英文名:"); 17 String eName=sc.next(); 18 19 if(student.containsKey(eName)) { 20 Student students=student.get(eName); 21 System.out.println(eName+"对应的学员姓名是:"+students.getName()+";性别是:"+students.getGender()); 22 }else { 23 System.out.println("没有找到"+eName+"学员的信息"); 24 } 25 26 } 27 }
结果