gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:34:56 25 4
gpt4 key购买 nike

我有一个具有如下功能的类:

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.

这是错误的输出。我的预期输出会说第一个航类的出发地不存在。

最佳答案

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

关于java - 函数未返回正确的值,正在列表中查找值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22418165/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com