函数未返回正确的值,在列表中查找值

时间:2014-03-15 00:46:50

标签: java

我有一个具有如下函数的类:

public void createFlight(String aname, String orig, String dest, String id) {
    Flight f = new Flight(aname, orig, dest, id);
    boolean found = false;
    boolean quit = false;
    for (int i = 0; i < flightList.size(); i++) {
        //Check origin exist
        if (airportList.get(i).getName().equals(orig))
            found = true;
        if (!found) {
            System.out.println("Origin " + orig + " does not exist.");
            quit = true;
        }
        found = false;
        //check destination exists
        if (airportList.get(i).getName().equals(dest))
            found = true;
        if (!found) {
            System.out.println("Destination " + dest + " does not exist.");
            quit = true;
        }
        //check if origin and destination are the same
        if (orig.equals(dest)) {
            System.out.println(id + "'s Origin and destination cannot be the same.");
            quit = true;
        }
    }
    if (!quit) {
        flightList.add(f);
        System.out.println("Added flight " + id);
    }
}

如果我跑:

    s.createFlight("USAIR", "ACE", "YVR", "778");//origin airport does not exist
    s.createFlight("BOS", "YHZ", "YUL", "123");

第2行应该正常工作,并且第1行的原始机场不存在。但是我得到了这个:

Added flight 778
Destination YUL does not exist.

哪个输出不正确。我的预期输出会说第一个航班的起源不存在。

4 个答案:

答案 0 :(得分:1)

不确定是什么,但循环变量&#34; i&#34;应该从0运行到airPortList.size()我认为。

答案 1 :(得分:1)

在检查原点是否存在之前,您需要在循环开始时将found重置为false;否则它可能会在上一次迭代的目的地检查中留下true的值。

for (int i = 0; i < flightList.size(); i++) {
    found = false; // <-- add this
    //Check origin exist
    if (airportList.get(i).getName().equals(orig))
        found = true;
    ...

按照现在的方式,当在航班列表中进行迭代时,如果存在一个航班的目的地,则其行为就像下一个航班的起源存在一样,无论如何是否确实存在。

然而,这个逻辑总体上有点奇怪,并且一些简化(例如Mohammad's answer)可以使它更清晰,并且更容易诊断这样的问题。此外,如R Kaja Mohideen提到的答案,您可能无法搜索整个机场列表。

答案 2 :(得分:1)

这种简化有帮助吗?你能弄清楚你做错了吗?

public void createFlight(String aname, String orig, String dest, String id) {
    Flight f = new Flight(aname, orig, dest, id);
    boolean found = false;
    boolean quit = false;

    for (int i = 0; i < flightList.size(); i++) {
        //Check origin exist
        if (!airportList.get(i).getName().equals(orig))
        {
            System.out.println("Origin " + orig + " does not exist.");
            quit = true;
        }        
        //check destination exists
        if (!airportList.get(i).getName().equals(dest))
        {
            System.out.println("Destination " + dest + " does not exist.");
            quit = true;
        }
        //check if origin and destination are the same
        if (orig.equals(dest)) {
            System.out.println(id + "'s Origin and destination cannot be the same.");
            quit = true;
        }
    }
    if (!quit) {
        flightList.add(f);
        System.out.println("Added flight " + id);
    }
}

以下是您可以做的事情:

public void createFlight(String aname, String orig, String dest, String id) {
    Flight f = new Flight(aname, orig, dest, id);
    boolean found = false;
    boolean quit = false;


    for (int i = 0; i < flightList.size(); i++) {
        //Check origin exist
        if (!airportList.get(i).getName().equals(orig))
        {
            System.out.println("Origin " + orig + " does not exist.");
            quit = true;
            break;
        }        
        //check destination exists
        else if (!airportList.get(i).getName().equals(dest))
        {
            System.out.println("Destination " + dest + " does not exist.");
            quit = true;
            break;
        }
        //check if origin and destination are the same
        else if (orig.equals(dest)) {
            System.out.println(id + "'s Origin and destination cannot be the same.");
            quit = true;
            break;
        }
    }
    if (!quit) {
        flightList.add(f);
        System.out.println("Added flight " + id);
    }
}

答案 3 :(得分:1)

for loop正在遍历Flight中的所有flightList,而不是Airport列表中的所有Airport

如果flightList中的Airport和100 airportList中有三个航班,则循环中的逻辑只会将提供的航班与前3个机场进行比较而不是全部100个您应该使用airportList来确定迭代次数,或者更好地将Airport放在密钥是机场代码的Map<String,Airport>内。这将消除代码中的许多标志,并使条件更简洁。

Map<String,Airport> airportMap = new HashMap<String,Airport>();
/* Populate map like airportMap.put("BOS", new Airport("BOS",..));*/

public void createFlight(String aname, String orig, String dest, String id) {

    if(airportMap.containsKey(orig) && airportMap.containsKey(dest) && !orig.equals(dest)){
         Flight f = new Flight(aname, orig, dest, id);
         flightList.add(f);
         System.out.println("Added flight " + id);
    }else if(orig.equals(dest)){
         System.out.println(id + "'s Origin and destination cannot be the same.");
    }else if(!airportMap.containsKey(orig)){
         System.out.println("Origin " + orig + " does not exist.");
    }else if(!airportMap.containsKey(dest)){
         System.out.println("Destination " + dest + " does not exist.");
    }   
}
相关问题