You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java航班程序:如何输出中转城市的关联城市与总距离?

问题修正方案

你的showConnections方法核心问题是未利用传入的中转节点信息,反而遍历了整个图的所有节点,导致输出所有城市的关联航线。以下是针对性的修正方案,同时满足展示中转城市关联节点及总距离的需求。

修改逻辑说明

  1. 直接通过中转节点的标签获取其所有关联节点,无需遍历全图
  2. 计算总距离:起点到中转的距离(即传入的layOverVertex的weight) + 中转到目标节点的距离
  3. 优化输出格式,清晰展示完整航线及总距离构成

修改后的完整代码

修正后的HWDriverPrep.java

import java.util.ArrayList;
import java.util.Scanner;
import java.util.Set;

public class HWDriverPrep {
    public static void main(String[] args) {
        HWGraph g = createGraph();
        printGraph(g);
        Set<String> keys = g.getKeys();
        String startCity = "Chicago";
        String layOver = getLayOverCity(keys, startCity);
        Vertex v = g.getThisVertex(startCity, layOver);
        
        // 处理无直达航线的异常情况
        if (v == null) {
            System.out.println("\n错误:起点" + startCity + "到中转城市" + layOver + "无直达航线!");
            return;
        }
        
        System.out.printf("\n起点:%s 到中转城市:%s 的直达距离: %d\n", startCity, v.label, v.weight);
        // 传入起点城市用于总距离计算和输出
        showConnections(g, v, startCity);
    }

    private static void printGraph(HWGraph g) {
        g.graphMap.forEach((key, value) -> show(key, value));
    }

    private static void show(String key, ArrayList<Vertex> value) {
        System.out.println("城市 " + key + " 的关联航线--------");
        for (Vertex v : value) {
            System.out.println("目标城市: " + v.label + " 距离: " + v.weight);
        }
    }

    private static String getLayOverCity(Set<String> keys, String startCity) {
        Scanner s = new Scanner(System.in);
        String oStr = String.join(", ", keys);
        System.out.printf("\n请选择中转城市(可选:%s): ", oStr);
        return s.nextLine().trim();
    }

    // 修正后的showConnections方法
    private static void showConnections(HWGraph g, Vertex layOverVertex, String startCity) {
        // 获取中转城市标签
        String layoverCity = layOverVertex.label;
        // 获取中转城市的所有关联节点
        ArrayList<Vertex> connections = g.getConnections(layoverCity);
        
        System.out.printf("\n中转城市 %s 的所有关联航线(含总距离):\n", layoverCity);
        for (Vertex v : connections) {
            // 计算总距离:起点→中转 + 中转→目标
            int totalDistance = layOverVertex.weight + v.weight;
            System.out.printf("- %s→%s→%s | 总距离: %d (%d+%d)\n", 
                              startCity, layoverCity, v.label, totalDistance, 
                              layOverVertex.weight, v.weight);
        }
    }

    private static HWGraph createGraph() {
        HWGraph g = new HWGraph();
        g.addVertex("Chicago");
        g.addVertex("Dallas");
        g.addVertex("Atlanta");
        g.addVertex("New York");
        g.addVertex("Houston");
        g.addVertex("Orlando");

        // 添加航线
        g.addEdge("Chicago", new Vertex("Dallas", 968));
        g.addEdge("Chicago", new Vertex("Atlanta", 718));
        g.addEdge("Chicago", new Vertex("New York", 790));

        g.addEdge("Dallas", new Vertex("Houston", 239));
        g.addEdge("Dallas", new Vertex("Orlando", 1120));

        g.addEdge("Houston", new Vertex("Orlando", 967));

        g.addEdge("Atlanta", new Vertex("Dallas", 781));
        g.addEdge("Atlanta", new Vertex("New York", 870));
        g.addEdge("Atlanta", new Vertex("Orlando", 438));

        g.addEdge("New York", new Vertex("Houston", 1647));
        g.addEdge("New York", new Vertex("Orlando", 1080));

        return g;
    }
}

其他文件无需修改

Vertex.java和HWGraph.java保持原有代码即可。

额外优化点

  • 增加了空指针判断,避免用户选择无直达航线的中转城市时程序崩溃
  • 简化了中转城市选择的字符串拼接逻辑
  • 输出格式更清晰,完全匹配你给出的示例(如芝加哥→纽约→奥兰多的总距离展示)

内容的提问来源于stack exchange,提问作者xkellss

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 07:05:21