import java.util.*;

public class SubwayDemo {
    public static void main(String[] args) {
        // 1.将对应关系存储到map中
        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, '新郑机场站');

        // 遍历打印map集合
        for (Map.Entry<Integer, String> entry : stationMap.entrySet()) {
            System.out.println(entry.getKey() + '=' + entry.getValue());
        }

        // 2.移除编号为11的车站
        stationMap.remove(11);

        // 3.将所有编号取出放到一个单列集合noList中,遍历打印noList集合中的数据
        List<Integer> noList = new ArrayList<>(stationMap.keySet());
        for (Integer no : noList) {
            System.out.print(no + ' ');
        }
        System.out.println();

        // 4.从noList中随机取一个编号,拿到对应的车站名打印到控制台
        int randomNo = noList.get(new Random().nextInt(noList.size()));
        String stationName = stationMap.get(randomNo);
        System.out.println(randomNo + '=' + stationName);

        // 5.计算地铁票价规则
        Scanner scanner = new Scanner(System.in);
        int startNo, endNo;
        String startName, endName;

        // 输入上车站
        while (true) {
            System.out.println('请输入上车站:');
            startName = scanner.nextLine();
            startNo = getKeyByValue(stationMap, startName);
            if (startNo != -1) {
                break;
            } else {
                System.out.println('您输入的上车站:' + startName + '不存在,请重新输入上车站:');
            }
        }

        // 输入到达站
        while (true) {
            System.out.println('请输入到达站:');
            endName = scanner.nextLine();
            endNo = getKeyByValue(stationMap, endName);
            if (endNo != -1) {
                break;
            } else {
                System.out.println('您输入的到达站:' + endName + '不存在,请重新输入到达站:');
            }
        }

        // 计算票价和耗时
        int distance = Math.abs(endNo - startNo);
        int cost;
        if (distance <= 3) {
            cost = 3;
        } else if (distance <= 5) {
            cost = 4;
        } else {
            cost = 4 + 2 * (distance - 5);
            cost = cost > 10 ? 10 : cost; // 封顶10元
        }
        int time = distance * 2;

        // 输出结果
        System.out.println('从' + startName + '到' + endName + '经过' + distance + '站收费' + cost + '元,大约需要 ' + time + '分钟');
    }

    /**
     * 通过value获取map的key
     */
    public static int getKeyByValue(Map<Integer, String> map, String value) {
        for (Map.Entry<Integer, String> entry : map.entrySet()) {
            if (entry.getValue().equals(value)) {
                return entry.getKey();
            }
        }
        return -1;
    }
}

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

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