package cd_one.code16.exer2;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class CityMapTest {
public static void main(String[] args) {
//1.获取map,并且遍历map中的所有key
Map map = CityMap.model;
Set provinces = map.keySet();
for (Object province : provinces) {
System.out.print(province + "\t\t");
}
//2.根据提示,从键盘中获取省份值,如果省份存在,则遍历value中的各个城市的值
//如果不存在,则提示重新输入
Scanner sc = new Scanner(System.in);
String[] cities;
while(true){
System.out.println("\n请输入你所在的省份:");
String province = sc.next();
// 如果存在则返回你对应的数组cities//为什么报错呢
//get的是object,下面的也是object,但我们自己知道是String类型
cities = (String[]) map.get(province);
if(cities == null || cities.length == 0){
System.out.println("你输入的省份有误,请重新输入");
}else{
break;//意味着用户输入的省份是存在的,则跳出当前循环
}
}
for (int i = 0; i < cities.length; i++) {
System.out.print(cities[i] + "\t\t");
}
//3、根据提示,从键盘获取城市,遍历各个城市构成的String[],判断输入的城市是否存在于此数组中
//如果存在,信息登记完毕,如果不存在,表示用户重新输入
l:while(true){
System.out.println("\n请选择你所在的城市:");
String city = sc.next();
//方式1:
// for (int i = 0; i < cities.length; i++) {
// if(city.equals(cities[i])){
// System.out.println("信息登记完毕");
// break l;
// }
// }
// System.out.println("输入的城市有误,请重新输入!");
//方式2:
if(containsCity(cities,city)){
System.out.println("信息登记完毕!");
break;
}
}
sc.close();
}
//第二种方式处理城市是否存在
public static boolean containsCity(String[] cities,String city){
for (int i = 0; i < cities.length; i++) {
if(city.equals(cities[i])){
return true;
}
}
return false;
}
}
class CityMap {
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[] {"济南","青岛","日照","临沂","泰安","聊城","德州"});
}
}
【无标题】
该文章介绍了如何使用Java编程语言创建一个CityMapTest类,通过HashMap来存储中国部分省份及其下辖城市。程序通过Scanner获取用户输入的省份和城市,检查并验证输入的正确性。
摘要由CSDN通过智能技术生成