Map<Integer, String> stationMap = new HashMap<>(); stationMap.put(1, '西流湖'); stationMap.put(2, '西三环'); stationMap.put(3, '秦岭路'); stationMap.put(4, '桐柏路'); stationMap.put(5, '碧沙岗'); stationMap.put(6, '绿城广场'); stationMap.put(7, '医学院'); stationMap.put(8, '火车站'); stationMap.put(9, '二七广场'); stationMap.put(10, '人民路'); // stationMap.put(11, '新郑机场站'); for (Map.Entry<Integer, String> entry : stationMap.entrySet()) { System.out.println(entry.getKey() + "=" + entry.getValue()); }

stationMap.remove(11);

List noList = new ArrayList<>(stationMap.keySet()); for (Integer no : noList) { System.out.println(no); }

Random random = new Random(); int randomNo = noList.get(random.nextInt(noList.size())); String stationName = stationMap.get(randomNo); System.out.println(randomNo + "=" + stationName);

Scanner scanner = new Scanner(System.in); int price = 0; int time = 0; while (true) { System.out.println("请输入上车站:"); String start = scanner.nextLine(); System.out.println("请输入到达站:"); String end = scanner.nextLine(); if (!stationMap.containsValue(start) || !stationMap.containsValue(end)) { System.out.println("输入的站名不存在,请重新输入"); continue; } int startNo = 0; int endNo = 0; for (Map.Entry<Integer, String> entry : stationMap.entrySet()) { if (entry.getValue().equals(start)) { startNo = entry.getKey(); } if (entry.getValue().equals(end)) { endNo = entry.getKey(); } } int stationNum = Math.abs(startNo - endNo) + 1; if (stationNum <= 3) { price = 3; } else if (stationNum <= 5) { price = 4; } else { price = 4 + (stationNum - 5) * 2; if (price > 10) { price = 10; } } time = stationNum * 2; System.out.println("从" + start + "到" + end + "经过" + stationNum + "站收费" + price + "元,大约需要" + time + "分钟"); break; }

地铁站信息查询及票价计算系统

原文地址: https://www.cveoy.top/t/topic/n8g2 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录