[英]Problems with if/else statements nested in a while loop
該程序是使用OOP的標准飛行數據庫。 在此主要方法中,有5個執行不同任務的選項。 選項1和2可以正常工作,但是由於某些原因,選項3-5不會執行。 選項3-5的if / else if中的代碼不會運行,該程序僅會再次輸出主菜單。
boolean exit = false;
while (exit == false)
{
System.out.println("Now what would you like to do?");
System.out.println("1. Print out a flight's info");
System.out.println("2. Print out the number of flights through the static variable");
System.out.println("3. Change the departure time of a flight");
System.out.println("4. Change the departure gate of a flight");
System.out.println("5. Exit");
int choice = sc.nextInt();
if (choice == 1)
{
System.out.println("Which flight would you like to print (1 or 2)?");
int choice2 = sc.nextInt();
if (choice2 == 1)
{
f1.printFlight();
}
else if (choice2 == 2)
{
f2.printFlight();
}
else
{
System.out.println("Invalid choice");
}
}
else if (choice == 2)
{
System.out.println("This is the number of flights: ");
int numFlights = f1.getNumFlights();
System.out.println(numFlights);
}
else if (choice == 3)
{
System.out.println("Which flight would you like to change the departure time of (1 or 2)?");
int choice3 = sc.nextInt();
System.out.println(choice3);
if (choice3 == 1)
{
System.out.println("What is the new departure time for flight " + choice3);
int newTime = sc.nextInt();
f1.changeDeptTime(newTime);
}
else if (choice3 == 2)
{
f2.changeDeptTime();
}
else
{
System.out.println("Invalid choice");
}
}
else if (choice == 4)
{
System.out.println("Which flight would you like to change the departure gate of (1 or 2)?");
int choice4 = sc.nextInt();
if (choice4 == 1)
{
f1.changeDeptGate();
}
else if (choice4 == 2)
{
f2.changeDeptGate();
}
else
{
System.out.println("Invalid choice");
}
}
else if (choice == 5)
{
System.out.println("Exit Reached");
exit = true;
}
}
使用switch()語句選擇每種情況。 喜歡
switch(x) {
case 1:
doSomething1();
case 2:
doSomething2();
case 3:
doSomething3();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.