当前位置: 首页 > 知识库问答 >
问题:

程序在单步执行时工作,但在运行时不工作

景温书
2023-03-14

我正在写一个程序,显示如下所示的航班信息:

package d.airlineData.engine;

import java.time.Duration;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map.Entry;

import a.airlineData.exceptions.NoFlightsException;
import c.airlineData.exceptions.NoAirportException;
import d.airlineData.graph.Airport;
import d.airlineData.graph.Flight;
import d.airlineData.graph.FlightGraph;

public class FlightGrapher {

    private FlightGraph fg;

    public FlightGrapher() throws NoFlightsException {      
        throw new NoFlightsException();
    }

    public FlightGrapher(ArrayList<Flight> flights) throws NoFlightsException  {
        fg = new FlightGraph();

        for(Flight flight: flights) {
            fg.addAirport(flight.getAirportA());
            fg.addAirport(flight.getAirportB());

            try {
                fg.addFlight(flight);
            } catch (NoAirportException e) {
                System.err.println(e.getMessage());
                e.printStackTrace();
            }
        }
    }

    public void printAll() {
        HashSet<Airport> airports = fg.getAirports();

        for(Airport airport : airports) {
            System.out.println(airport + ":");
            try {
                Iterator<Flight> it = fg.getFlightsFor(airport);
                while(it.hasNext()) {
                    System.out.println("\t" + it.next());
                    System.out.println();
                }

            } catch (NoAirportException e) {
                System.err.println(e.getMessage() + " while attempting to get flights for " + airport);
                e.printStackTrace();
            }
        }

    }
        public void printItinerary(Airport airportA, Airport airportB) {
    System.out.println("Leg\tLeave\t\tAt\tOn\tArrive\tAt");
    printItinerary(airportA,airportB, 1, 0.0, Duration.ofHours(0));
}

private void printItinerary(Airport airportA, Airport airportB, int leg, double totalPrice, Duration totalDuration) {
    Iterator<Flight> aFlights = airportA.getOutgoingFlights();
    System.err.println("Enters method printItinerary");
    System.err.println("airportA "  + airportA);
    System.err.println("airportB "  + airportB);
    System.err.println("leg " + leg);
    System.err.println("total price " + totalPrice);
    System.err.println("Duration " + totalDuration.toMinutes() + "mins");
    System.err.println();
    while(aFlights.hasNext()) {

        Flight currentFlight = aFlights.next();
        System.err.println("Enters while of printItinerary currentFlight: ");
        System.err.println(currentFlight);
        if(currentFlight.getAirportB().equals(airportB)) {

            System.out.println(leg + "\t" +
                    currentFlight.getAirportA() + "\t" +
                    currentFlight.getDepartureTime() + "\t" +
                    currentFlight.getFlightNumber() + "\t" +
                    currentFlight.getAirportB() + "\t" +
                    currentFlight.getArrivalTime());

            System.out.println();
            System.out.println("Total journey costs\t= £" + (currentFlight.getPrice() + totalPrice));
            System.out.println("Total time in air\t= " + (currentFlight.getFlightDuration().plus(totalDuration)));

            return;

        }else {
            System.err.println("enters else " + "currentFlight " + currentFlight.getAirportB() + " airport B " + airportB );
            System.err.println();
            if(hasAPath(currentFlight.getAirportB(), airportB)) {
                System.out.println(leg + "\t" +
                        currentFlight.getAirportA() + "\t" +
                        currentFlight.getDepartureTime() + "\t" +
                        currentFlight.getFlightNumber() + "\t" +
                        currentFlight.getAirportB() + "\t" +
                        currentFlight.getArrivalTime());

                printItinerary(currentFlight.getAirportB(), airportB, leg + 1, 
                        (currentFlight.getPrice() + totalPrice), 
                        (currentFlight.getFlightDuration().plus(totalDuration)));   
            }
        }
    }       
}

private boolean hasAPath(Airport airportA, Airport airportB) {
    System.err.println("Enters hasAPath with airportA " + airportA + " airportB " + airportB);
    Iterator<Flight> aFlights = airportA.getOutgoingFlights();
    while(aFlights.hasNext()) {
        Flight currentFlight = aFlights.next();
        System.err.println("Enters while of hasAPath currentFlight: ");
        System.err.println(currentFlight);
        if(currentFlight.getAirportB().equals(airportB)) {  
            System.err.println("returns true for airportA " + airportA + " airportB " + airportB );
            return true;            
        }else {
            System.err.println("Calls hasAPath with airportA " + currentFlight.getAirportB() + " airportB " + airportB);
            return hasAPath(currentFlight.getAirportB(), airportB);
        }

    }
    System.err.println("returns false for airportA " + airportA + " airportB " + airportB );
    return false;
}

    public static void main(String[] args) {
        ArrayList<Flight> flights = new ArrayList<>();
        HashMap<String, Airport> airports = new HashMap<>();

        airports.put("Edinburgh", new Airport("Edinburgh")); 
        airports.put("Heathrow", new Airport("Heathrow")); 
        airports.put("Amsterdam", new Airport("Amsterdam")); 
        airports.put("Boston", new Airport("Boston")); 
        airports.put("Montreal", new Airport("Montreal")); 
        airports.put("Chicago", new Airport("Chicago")); 
        airports.put("Toronto", new Airport("Toronto")); 
        airports.put("New Delhi", new Airport("New Delhi")); 
        airports.put("Shanghai", new Airport("Shanghai")); 
        airports.put("Hong Kong", new Airport("Hong Kong")); 

        flights.add(new Flight(airports.get("Edinburgh"),airports.get("Heathrow"),
                "B7982",LocalTime.of(22,10),LocalTime.of(23,15), 110.0));

        flights.add(new Flight(airports.get("Heathrow"),airports.get("Amsterdam"),
                "B7982",LocalTime.of(22,10),LocalTime.of(23,15), 100.0));

        flights.add(new Flight(airports.get("Heathrow"),airports.get("Boston"),
                "B7982",LocalTime.of(22,10),LocalTime.of(23,15), 230.0));

        flights.add(new Flight(airports.get("Boston"),airports.get("Chicago"),
                "B7982",LocalTime.of(22,10),LocalTime.of(23,15), 150.0));

        flights.add(new Flight(airports.get("Boston"),airports.get("Montreal"),
                "B7982",LocalTime.of(22,10),LocalTime.of(23,15), 100.0));

        flights.add(new Flight(airports.get("Montreal"),airports.get("Toronto"),
                "B7982",LocalTime.of(22,10),LocalTime.of(23,15), 90.0));

        flights.add(new Flight(airports.get("Edinburgh"),airports.get("Chicago"),
                "B7982",LocalTime.of(22,10),LocalTime.of(23,15), 560.0));

        flights.add(new Flight(airports.get("New Delhi"),airports.get("Shanghai"),
                "B7982",LocalTime.of(22,10),LocalTime.of(23,15), 430.0));

        flights.add(new Flight(airports.get("Shanghai"),airports.get("Hong Kong"),
                "B7982",LocalTime.of(22,10),LocalTime.of(23,15), 230.0));


        Iterator<Entry<String,Airport>> airportIt = airports.entrySet().iterator();

        while(airportIt.hasNext()) {
            Entry<String, Airport> pair = airportIt.next();
            Airport airport = pair.getValue();
            for(Flight flight: flights) {
                if(flight.getAirportA().equals(airport)) {
                    airport.addOutgoingFlight(flight);
                }
            }
        }

        try {
            FlightGrapher fg = new FlightGrapher(flights);
            //fg.printAll();
            fg.printItinerary(airports.get("Edinburgh"), airports.get("Toronto")); // steps into this method
        } catch (NoFlightsException e) {
            System.err.println(e.getMessage() + " when trying to make a flight between a nonexistant airport");
            e.printStackTrace();
        }

    }

}

我的问题是使用方法时:

printItinerary(Airport airportA, Airport airportB, int leg, double totalPrice, Duration totalDuration)

当正常运行时,由于某种原因它不会执行,但是当我使用调试器执行程序时,一切都执行得很好,我得到了一个有意义的输出(格式不是很好,但我可以使用),为什么会发生这种情况?

输出应该是这样的:

Leg    Leave      At    On    Arrive    At
1      Edinburgh  10:30 BA345 Heathrow  11:30
2      Heathrow   14:00 BA657 Boston    15:30
3      Boston     18:00 AA652 Montreal  19:30
4      Montreal   22:00 AA216 Toronto   23:30

Total Journey Cost = £530
Total Time in the Air = 4 hrs 20 min

当我正常运行时,我会得到这个:

Leg    Leave      At    On    Arrive    At

这是当我一步虽然(或运行调试):

Leg     Leave           At      On      Arrive      At
1       Edinburgh       22:10   B7982   Heathrow          23:15
2       Heathrow        22:10   B7982   Boston      23:15
3       Boston  22:10   B7982   Montreal            23:15
4       Montreal        22:10   B7982   Toronto     23:15

Total journey costs = £530.0
Total time in air   = PT4H20M

我希望输出的是单步执行部分(稍后我将处理格式化)

eddit:我已经在错误流中添加了很多输出,这是我运行它时的输出。。。出于某种原因,它在“hasAPath”方法中停止:

Leg Leave       At  On  Arrive  At
Enters method printItinerary
airportA Edinburgh
airportB Toronto
leg 1
total price 0.0
Duration 0mins

Enters while of printItinerary currentFlight: 
Flight between Edinburgh & Chicago:
        For: £560.0
        Flight Number: B7982
        Leaves at: 22:10
        Arrives at: 23:15
        Duration: 1hr 5min
enters else currentFlight Chicago airport B Toronto

Enters hasAPath with airportA Chicago airportB Toronto
returns false for airportA Chicago airportB Toronto
Enters while of printItinerary currentFlight: 
Flight between Edinburgh & Heathrow:
        For: £110.0
        Flight Number: B7982
        Leaves at: 22:10
        Arrives at: 23:15
        Duration: 1hr 5min
enters else currentFlight Heathrow airport B Toronto

Enters hasAPath with airportA Heathrow airportB Toronto
Enters while of hasAPath currentFlight: 
Flight between Heathrow & Amsterdam:
        For: £100.0
        Flight Number: B7982
        Leaves at: 22:10
        Arrives at: 23:15
        Duration: 1hr 5min
Calls hasAPath with airportA Amsterdam airportB Toronto
Enters hasAPath with airportA Amsterdam airportB Toronto
returns false for airportA Amsterdam airportB Toronto

在这一点上,它应该返回,然后检查下一个航班的路径,下一个航班将是波士顿和多伦多,然后检查所有波士顿的航班,看看他们的机场是否与多伦多有联系...等等

共有1个答案

孙梓
2023-03-14

问题解决了。答案是,当你运行在调试它运行虽然所有的可能性,即使你返回答案,这就是为什么它评估为真和工作,我改变了有一个路径方法从这:

private boolean hasAPath(Airport airportA, Airport airportB) {
    Iterator<Flight> aFlights = airportA.getOutgoingFlights();
    while(aFlights.hasNext()) {
        Flight currentFlight = aFlights.next();
        if(currentFlight.getAirportB().equals(airportB)) {         
            return true;            
        }else {             
            return hasAPath(currentFlight.getAirportB(), airportB);
        }

    }

    return false;
}

为此:

private boolean hasAPath(Airport airportA, Airport airportB) {
    Iterator<Flight> aFlights = airportA.getOutgoingFlights();
    while(aFlights.hasNext()) {
        Flight currentFlight = aFlights.next();
        if(currentFlight.getAirportB().equals(airportB)) {              
            return true;            
        }else {
            if(hasAPath(currentFlight.getAirportB(), airportB)) {
                return true;
            }else {
                continue;
            }
        }

    }
    return false;
} 

如果其他人正在研究这个问题,那么我建议在代码的每个块将输出放置到控制台,并跟踪正在发生的事情,调试模式下的执行模型必须不同于正常运行时的执行模型。

 类似资料:
  • 这些天我们一直在尝试安装hadoop集群。有时成功,但大多数时候失败了。我根据官方文件和一些看似高质量的博客进行配置。 我遇到的问题是:所有进程(包括namenode、datanode、nodemanager、resourcemanager)都可以通过命令查看: 但是奴隶们实际上没有工作。我无法在web界面master:8088或master:50070中看到它们 有人说这是重复的namenode

  • 我最近决定开始使用空校验和注释(),我使用Intellij Idea的注释库来访问这些注释。我试图弄清楚这些注释在运行时是否可以检查值是否为null,如果是,它如何处理这些错误(例如,它是否抛出,它是否只返回默认值(例如null,0,false))。此外,如果这些注释不起作用,是否存在一组在运行时起作用的更标准化注释(例如,)?如果这些不起作用,我应该停止使用它们并返回标准的空校验和(),还是应该

  • 空手道afterFeature函数在本地运行时运行正常,但在Jenkins中运行时失败,我得到。而应为。 代码片段 主要的功能片段 cleanup.feature Jenkins的日志:的断言失败,但它没有记录响应状态的实际值。 此外,我没有看到在Jenkins中执行postFeature的日志,也不是我要做进一步分析的cucumber报告的一部分。

  • 我正在尝试将我的程序导出为一个可运行的JAR。该程序在eclipse中工作得非常好,但它不能作为一个可运行的JAR运行。我正在使用另外3个jar文件作为引用jar,这样我就可以使用音频,我认为这可能是问题所在。可运行的jar启动,但它只是一个全白的窗口,程序没有启动。 我点击我的项目,然后右键点击并选择“导出”,然后我选择“可运行的JAR”选项。我尝试使用所有三个处理引用库的选项来创建jar。 将

  • 我目前正试图用Python通过通用串行总线写入我的一个Arduino Nano。然而,我发现(使用完全相同的代码),当我将代码键入IDLE时,代码工作得很好,但是当我将其保存到一个文件并试图从那里运行时,由于某种原因,Arduino永远不会收到数据。我检查了两个位置,都使用了正确的Python版本(2.7.9)(不幸的是,由于我正在使用的其他库,我不能使用Python 3)。 我使用的代码是: 当

  • 我使用ByteBuddy创建了一个java代理用于方法日志记录。 具有用。但是,在这些方法中,我引用了一个公共静态类及其所有属性和方法公共静态。在运行应用程序时,我能够使用这种方法记录所有方法,没有问题。 但是,当我运行一个用注释的测试类时,出现了一个问题。出于某种原因,插入指令的代码似乎消失了。还有其他人经历过类似的问题吗? 作为参考,当在IntelliJ中运行测试类时,我做了一个,并且在测试类