11.MAP查询每年的世界杯冠军

来源:互联网 发布:usb网络打印服务器 编辑:程序博客网 时间:2024/05/05 09:59

/*
(Map)利用Map,完成下面的功能:
从命令行读入一个字符串,表示一个年份,输出该年的世界杯冠军是哪支球队。如果该
年没有举办世界杯,则输出:没有举办世界杯。

*/


import java.util.*;


class ChampionMap{
Map<String ,String> a = new HashMap<String, String>();
 boolean bl;
     void  addChampion(){
     
          a.put("2006","意大利");
          a.put("2002","巴西");
          a.put("1998","法国");
          a.put("1994","巴西");
          a.put("1990","德国");
          a.put("1986","阿根廷");
          a.put("1982","意大利");
          a.put("1978","阿根廷");
          a.put("1974","德国");
          a.put("1970","巴西");
          a.put("1966","英格兰");
          a.put("1962","巴西");
          a.put("1958","巴西");
          a.put("1954","德国");
          a.put("1950","乌拉圭");
          a.put("1938","意大利");
          a.put("1934","意大利");
          a.put("1930","乌拉圭");
     
     
     }
    void checkChampion(){
        Scanner sc= new Scanner(System.in);
   System.out.println("Input year");
    String str = sc.next();
          bl = a.containsKey(str);
        
        if(bl==true) {
            System.out.println(a.get(str));
            }
            else
            System.out.println("没有举办世界杯");
        }
        void checkChampionName(){
            private int count=0;
            Scanner sc = new Scanner (System.in);
            System.out.println("Input Country Name");
            String str=sc.next();
        Set<Map.Entry<String,String>> mapEntry = a.entrySet();
           Iterator <Map.Entry<String,String>> it= mapEntry.iterator();
           while(it.hasNext()){
               
               Map.Entry<String,String> me =it.next();
               
                if(me.getValue().equals(str)){
                    count++;
                    System.out.println("year="+me.getKey());
                    }
                    
         
              
              
                                
               
               }
               if(count==0){
                   System.out.println("没有获得过世界杯");
                   }
            
            }
    
    }
public  class D1{
 

public static void main(String args[]){
 

   ChampionMap cl= new ChampionMap();
        cl.addChampion();
    //    cl.checkChampion();
        cl.checkChampionName();
    }

    
}





原创粉丝点击