Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 嵌套在while循环中的if/else语句存在问题_Java_If Statement_While Loop_Boolean - Fatal编程技术网

Java 嵌套在while循环中的if/else语句存在问题

Java 嵌套在while循环中的if/else语句存在问题,java,if-statement,while-loop,boolean,Java,If Statement,While Loop,Boolean,该程序是一个使用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 fli

该程序是一个使用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();
}
使用switch()语句进行每种情况的选择。像

switch(x) {
    case 1:
        doSomething1();
    case 2:
        doSomething2();
    case 3:
        doSomething3();
}

您能连接输出块吗?您输入的输入选项是否有特定的顺序?如果你在3,4之前做1和2。或者5,或者如果您先输入3、4或5,在这两种情况下是否仍会出现问题?您希望打印哪个航班(1或2)?航班名称:A目的地:纽约起飞时间:A1登机口:上午11:00现在您想做什么?1.打印出航班信息2。通过静态变量3打印航班数。更改航班4的起飞时间。更改5号航班的登机门。3号出口您想更改(1或2)的起飞时间?现在你想做什么?1.打印出航班信息2。通过静态变量3打印航班数。改变航班的起飞时间等@无论顺序如何,问题仍然会发生。我认为这与选项3、4和5的扫描仪有关也许@DBugI将其剪切/粘贴到一个小测试程序中,所有5个选项都正常工作。您可能需要显示整个程序(如果不是太大的话)。是否可以附加输出块?输入选项是否有特定的顺序?如果你在3,4之前做1和2。或者5,或者如果您先输入3、4或5,在这两种情况下是否仍会出现问题?您希望打印哪个航班(1或2)?航班名称:A目的地:纽约起飞时间:A1登机口:上午11:00现在您想做什么?1.打印出航班信息2。通过静态变量3打印航班数。更改航班4的起飞时间。更改5号航班的登机门。3号出口您想更改(1或2)的起飞时间?现在你想做什么?1.打印出航班信息2。通过静态变量3打印航班数。改变航班的起飞时间等@无论顺序如何,问题仍然会发生。我认为这与选项3、4和5的扫描仪有关也许@DBugI将其剪切/粘贴到一个小测试程序中,所有5个选项都正常工作。您可能需要显示整个程序(如果不是太大的话)。给出答案时,最好是给出一个。给出答案时,最好是给出一个。