package test7; public class Penguin { private String name; private String 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; } public Penguin(String name, String gender) { super(); this.name = name; this.gender = gender; } }
package test7; import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Test2 { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { Map<String, String> map = new HashMap<String, String>(); map.put("欧欧", "Q仔"); map.put("亚亚", "Q妹"); map.put("菲菲", "Q妹"); map.put("美美", "Q妹"); System.out.println("共有" + map.size() + "只企鹅。"); System.out.println("分别是:"); for (Map.Entry<String, String> penguin : map.entrySet()) { System.out.println(penguin.getKey() + " " + penguin.getValue()); } System.out.println("请输入您要删除的企鹅的姓名:"); String choose = sc.next(); // for (Map.Entry<String, String> penguin: map.entrySet()) { // // if(!penguin.getKey().equals(choose)) { // // System.out.println("找不到您要删除的企鹅,请重新输入:"); // sc.next(); // }else { // // } // } while (!map.containsKey(choose)) { System.out.println("找不到您要删除的企鹅,请重新输入:"); choose = sc.next(); // return; } map.remove(choose); System.out.println("删除成功!"); System.out.println("删除后还有:" + map.size() + "只企鹅"); System.out.println("分别是:"); for (Map.Entry<String, String> penguin : map.entrySet()) { System.out.println(penguin.getKey() + " " + penguin.getValue()); } } }