java-Set exercises 1

Source: Internet
Author: User

The first problem (map) uses map to complete the following functions:

Reads a string from the command line, indicating a year, which team is the winner of the World Cup. If the World Cup is not held in that year, the output: No World Cup is held.

Attached: the World Cup title and the corresponding title year, please refer to the Appendix of this chapter. Appendix

    1. Previous World Cup champions

Number of sessions

Year held

Host Location

Champion

First session

1930

Uruguay

Uruguay

Second session

1934

Italy

Italy

Third session

1938

France

Italy

Session Four

1950

Brazil

Uruguay

Session Five

1954

Switzerland

Sid

Session VI

1958

Sweden

Brazil

vii

1962

Chile

Brazil

The first

1966

England

England

Ninth session

1970

Mexico

Brazil

Tenth session

1974

Former West German

Sid

11th session

1978

Argentina

Argentina

12th Session

1982

Spain

Italy

13th session

1986

Mexico

Argentina

14th session

1990

Italy

Sid

15th session

1994

United States

Brazil

16th session

1998

France

France

17th session

2002

Korean-Japanese

Brazil

18th session

2006

Germany

Italy

19th session

2010

South Africa

Spain

20th session

2014

Brazil

Germany

Based on the original World Cup map, add the following features: Read the name of a team and output a list of the years the team won the championship. For example, read "Brazil", should output 1958 1962 1970 1994 2002 read "Holland", should output did not win the World Cup

 PackageCom.jihelianxi;ImportJava.util.HashMap;ImportJava.util.Map;ImportJava.util.Scanner; Public classTESTSJB { Public Static voidMain (string[] args) {Map<String,String> ma=NewHashmap<>(); Ma.put ("1930", "Uruguay" ); Ma.put ("1934", "Italy" ); Ma.put ("1938", "Italy" ); Ma.put ("1950", "Uruguay" ); Ma.put ("1954", "Sid" ); Ma.put ("1958", "Brazil" ); Ma.put ("1962", "Brazil" ); Ma.put ("1966", "England" ); Ma.put ("1970", "Brazil" ); Ma.put ("1934", "Sid" ); Scanner SC=NewScanner (system.in); System.out.println ("Enter Year:"); String St=Sc.nextline (); if(Ma.containskey (st) = =true) {System.out.println (St+ "Crown Country is:" +Ma.get (ST)); }        Else{System.out.println (St+ "Not hosting the World Cup"); } Scanner SC1=NewScanner (system.in); System.out.println ("Please enter the Championship team"); String str1=Sc1.nextline (); if(Ma.containsvalue (str1) = =false) {System.out.println (str1+ "The team has not won the championship"); }        Else{System.out.println (str1+ "The winning year for the team is:");  for(String str:ma.keySet ()) {if(Ma.get (str). Equals (str1)) {System.out.print (str+" "); }            }                    }            //traversing the Keyset collection for value//For (String st:ma.keySet ())//        {//String B=ma.get (ST);//        }//        //System.out.println ("Input country name:");//String str=sc.nextline ();//for (int i=0;i<ma.size (); i++)//        {//if (b.equals (str))//            {//System.out.println (ST);//            }//Else//            {//System.out.println ("Did not win the World Cup");//            }//        }                            }}

java-Set exercises 1

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.