(Map)利用Map,完成下面的功能:  从命令行读入一个字符串,表示一个年份,输出该年的世界杯冠军是哪支球队。如果该 年没有举办世界杯,则输出:没有举办世界杯。  附:世界杯冠军以及对应的夺冠年份,请参考本章附录。 附录 (Map)在原有世界杯Map 的基础上,增加如下功能: 读入一支球队的名字,输出该球队夺冠的年份列表。 例如,读入“巴西”,应当输出 1958 1962 1970 1

package homework001;

import java.util.HashMap;
import java.util.Scanner;

public class Map {

    public static void main(String[] args) {
        
        
        java.util.Map<String, String> m1 = new HashMap<>();
        m1.put("1930", "乌拉圭");
        m1.put("1934", "意大利");
        m1.put("1938", "意大利");
        m1.put("1950", "乌拉圭");
        m1.put("1954", "西德");
        m1.put("1958", "巴西");
        m1.put("1962", "巴西");
        m1.put("1966", "英格兰");
        m1.put("1970", "巴西");
        m1.put("1974", "西德");
        m1.put("1978", "阿根廷");
        m1.put("1982", "意大利");
        m1.put("1986", "阿根廷");
        m1.put("1990", "西德");
        m1.put("1994", "巴西");
        m1.put("1998", "法国");
        m1.put("2002", "巴西");
        m1.put("2006", "意大利");
        m1.put("2010", "西班牙");
        m1.put("2014", "德国");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入年份");
        String str = sc.nextLine();
        if(m1.containsKey(str)==false)
        {
            System.out.println(str+"年没有举办世界杯");
        }
        else
        {
            System.out.println(str+"年的夺冠球队是:"+m1.get(str));
        }
        
        Scanner sc1 = new Scanner(System.in);
        System.out.println("请输入夺冠球队");
        String str1 = sc1.nextLine();
        if(m1.containsValue(str1)==false)
        {
            System.out.println(str1+"队没有获得过冠军");
        }
        else
        {
            System.out.println(str1+"队的夺冠年份是:");
            for(String k :m1.keySet())
            {
                if(m1.get(k).equals(str1))
                {
                    System.out.print(" "+k);
                    }
            }
            
        }
        

    }

}

原文地址:https://www.cnblogs.com/HRZJ/p/5912410.html