2014-02-26 128 views
0

我得到「的飛行提供匹配信息」 「記錄乘客的詳細信息和預訂完成」從for循環中的if退出for循環?

但後來我也越來越「有沒有航班飛往該目的地」

for (int k = 0;k <=4; k++) 
{ 
    if (destination.equalsIgnoreCase(flights[k].getDestination())) 
    { 
     k = 5; 

     System.out.print("\nEnter desired day of departure: "); 
     day = scan.nextLine(); 

     System.out.println("\f"); 

     if (day.equalsIgnoreCase(flights[k].getDay())) 
     { 

      if (flights[k].getBookedSeats() < 100) 
      { 

       passengers[0 + bookedSeats].setName(name); 
       passengers[0 + bookedSeats].setAddress(address); 
       passengers[0 + bookedSeats].setEmail(email); 
       passengers[0 + bookedSeats].setOnFlight(k); 
       flights[k].increaseBookedSeats(); 

       System.out.println("\nA flight is available matching this information"); 
       System.out.println("Passenger's details recorded and booking completed"); 

      }else{ 
       System.out.println("\nThere are no seats available on this flight"); 
      } 
     }else 
     { 
      System.out.println("\nThere are no flights flying to this destination on this day"); 
     } 
    }else if(!destination.equalsIgnoreCase(flights[k].getDestination()) && k==4) 
    { 
     System.out.println("\nThere are no flights flying to this destination"); 
    } 

} 
+0

這是什麼問題? – Kick

+0

你應該向我們展示'flight [k] .getBookedSeats()'做了什麼? – ItachiUchiha

+0

上帝原諒我,但你也可以用標籤打破;-) http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/ java/nutsandbolts/examples/BreakWithLabelDemo.java(ok,not really) – Leo

回答

4

您可以在if條件中添加break聲明以確保循環中斷。

+0

非常感謝。感謝幫助。 – user636363

+0

@ user636363不客氣! – Kakarot

0

當循環完成每次迭代運行之前,爲您的乘客找到有效的航班時,會出現此問題。在您的if語句中將k設置爲5是朝正確方向邁出的一步,但不起作用,因爲您在整個塊的其餘部分使用flights[5]

可以使用break語句而不是k = 5,但如果你想確保你的代碼將在今後易於維護,您可以通過使用while循環與當你是一個布爾值指定讓你的意圖明確完成。

int k = 0; 
bool done = false; 

while (!done && k <= 4) 
{ 
    if (destination.equalsIgnoreCase(flights[k].getDestination())) 
    { 
     k = 5; 

     System.out.print("\nEnter desired day of departure: "); 
     day = scan.nextLine(); 

     System.out.println("\f"); 

     if (day.equalsIgnoreCase(flights[k].getDay())) 
     { 

      if (flights[k].getBookedSeats() < 100) 
      { 

       passengers[0 + bookedSeats].setName(name); 
       passengers[0 + bookedSeats].setAddress(address); 
       passengers[0 + bookedSeats].setEmail(email); 
       passengers[0 + bookedSeats].setOnFlight(k); 
       flights[k].increaseBookedSeats(); 

       System.out.println("\nA flight is available matching this information"); 
       System.out.println("Passenger's details recorded and booking completed"); 

       // added this line: 
       done = true;   
      }else{ 
       System.out.println("\nThere are no seats available on this flight"); 
      } 
     }else 
     { 
      System.out.println("\nThere are no flights flying to this destination on this day"); 
     } 
    }else if(!destination.equalsIgnoreCase(flights[k].getDestination()) && k==4) 
    { 
     System.out.println("\nThere are no flights flying to this destination"); 
    } 

    k++; 
} 

有些人認爲使用break是好的,而且在許多情況下,我同意他們的觀點;但是,當您回過頭來查看您的代碼(或其他人)時,很高興知道for循環將絕對執行它指定的次數,但while循環可能會提早退出。這隻會讓事情變得更容易。

+0

在這種情況下,爲什麼不把這個額外的條件添加到'for'循環中呢?即,「bool done = false; for(int k = 0;!done && k <= 4; k ++){...}'。這似乎比隱藏在內部的增量循環更清潔。 –

+0

@JoshuaTaylor工作得很好,但它不是for循環的*精神*。重要的是你選擇一個約定(休息時間,複合條件等),並堅持不懈。 –

+0

你提出了一個解決方案,你可以在OP代碼中提供pblm嗎? – Kick