JAVA的使用集合框架实现省份城市-----JAVA

import java.util.*;

public class Prov
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        Map map = City.model;
        Set set = map.keySet();
        Iterator iterator = set.iterator();
        while(iterator.hasNext())
        {
            System.out.print(iterator.next() + "\t");
        }
        String[] cities;
        while(true)
        {
            System.out.println("\n请选择你所在的省份");
            String pro = in.next();
//            if(map.containsKey(pro))
//            {
//                map.get(pro);
//            }
//            else
//            {
//                System.out.println("输入有误");
//                break;
//            }
            cities = (String[])map.get(pro);
            if(cities == null || cities.length == 0)
            {
                System.out.println("输入有误");
            }
            else
            {
                for (int i = 0; i < cities.length; i++)
                {
                    System.out.print(cities[i] + "\t");
                }
            }
            l:while(true)
            {
                System.out.println("\n请选择你所在的城市");
                String city = in.next();
                for (int i = 0; i < cities.length; i++)
                {
                    if(cities[i].equals(city))
                    {
                        System.out.println("您所在的城市");
                        break l;
                    }
                    System.out.println("输入有误");
                }
            }
            in.close();
        }
    }
}

class City
{
    public static Map model = new HashMap();
    static {
        model.put("北京",new String[]{"北京"});
        model.put("辽宁",new String[]{"沈阳","盘锦","铁岭","安东","大连","锦州","松山"});
        model.put("吉林",new String[]{"长春","延边","吉林","白山","四平","松原"});
        model.put("河北",new String[]{"承德","沧州","邯郸","邢台","唐山","保定","石家庄"});
        model.put("河南",new String[]{"郑州","许昌","开封","洛阳","商丘","南阳","新乡"});
        model.put("山东",new String[]{"济南","青岛","日照","临沂","泰安","聊城","德州"});
    }
}

import java.util.*;

public class Prov
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        Map map = City.model;
        Set set = map.keySet();
        Iterator iterator = set.iterator();
        while(iterator.hasNext())
        {
            System.out.print(iterator.next() + "\t");
        }
        String[] cities;
        while(true)
        {
            System.out.println("\n请选择你所在的省份");
            String pro = in.next();
//            if(map.containsKey(pro))
//            {
//                map.get(pro);
//            }
//            else
//            {
//                System.out.println("输入有误");
//                break;
//            }
            cities = (String[])map.get(pro);
            if(cities == null || cities.length == 0)
            {
                System.out.println("输入有误");
            }
            else
            {
                for (int i = 0; i < cities.length; i++)
                {
                    System.out.print(cities[i] + "\t");
                }
            }
            l:while(true)
            {
                System.out.println("\n请选择你所在的城市");
                String city = in.next();
                for (int i = 0; i < cities.length; i++)
                {
                    if(cities[i].equals(city))
                    {
                        System.out.println("您所在的城市");
                        break l;
                    }
                    System.out.println("输入有误");
                }
            }
            in.close();
        }
    }
}

class City
{
    public static Map model = new HashMap();
    static {
        model.put("北京",new String[]{"北京"});
        model.put("辽宁",new String[]{"沈阳","盘锦","铁岭","安东","大连","锦州","松山"});
        model.put("吉林",new String[]{"长春","延边","吉林","白山","四平","松原"});
        model.put("河北",new String[]{"承德","沧州","邯郸","邢台","唐山","保定","石家庄"});
        model.put("河南",new String[]{"郑州","许昌","开封","洛阳","商丘","南阳","新乡"});
        model.put("山东",new String[]{"济南","青岛","日照","临沂","泰安","聊城","德州"});
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旧约Alatus

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值