考试第七题

 

 

复制代码
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());

        }
    }

}
复制代码

 

本文作者:AxeBurner

本文链接:https://www.cnblogs.com/bichen-01/p/11336503.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   AxeBurner  阅读(117)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
  1. 1 404 not found REOL
404 not found - REOL
00:00 / 00:00
An audio error has occurred.