[英]Function not returning proper value, looking for a value in a list
我有一個具有如下功能的類:
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()。
您需要在循環開始時將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的答案 )可以使它更清晰一些,並更容易診斷出此類問題。 此外,就像提到的R Kaja Mohideen的答案一樣,您可能並沒有搜索整個機場列表。
這種簡化有幫助嗎? 你能弄清楚你在做什么錯嗎?
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);
}
}
在for loop
是通過所有的迭代Flight
S IN的flightList
,而不是所有的Airport
S IN的Airport
名單。
如果flightList
有3個航班, flightList
100個Airport
,則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.");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.