我正在模拟一个只有一条跑道的机场。在繁忙时,想要起飞或着陆的飞机必须等待。 使用两个队列实现模拟,每个队列用于等待起飞和降落的飞机。 登陆具有更高的优先级。用户输入命令takeoffflightSymbol,landflightSymbol, 接下来,退出。前两个命令将航班放在适当的队列中。下一个命令 完成当前的起飞或降落,并启动下一个,打印动作(起飞或降落)和 飞行符号。但我似乎无法找到以下代码的问题。救命啊!
public class Runway
{
public Scanner in;
public Queue<String> takingOff;
public Queue<String> landing;
public Runway()
{
in = new Scanner(System.in);
boolean done = false;
while (!done)
{
System.out.println("Type TAKEOFF/LAND followed by Fight Number to Queue a plane");
System.out.println("Type NEXT to perform the next action, or QUIT to close the simulation");
String action = in.next();
if (action.equals("TAKEOFF"))
{
String flight = in.next();
takingOff.add(flight);
}
else if (action.equals("LAND"))
{
String flight = in.next();
landing.add(flight);
}
else if (action.equals("NEXT"))
{
handleNextAction();
}
else if (action.equals("QUIT"))
{
System.exit(0);
}
else {
System.out.println("ERROR - not a valid command");
}
}
}
public void handleNextAction()
{
if (landing.size() > 0)
{
String flight = landing.remove();
System.out.println("Flight " + flight + " is landing. ");
}
else if (takingOff.size() > 0)
{
String flight = takingOff.remove();
System.out.println("Flight " + flight + " is taking off.");
}
else
{
System.out.println("There are no flights waiting to take off or land");
}
}
public static void main (String[] args)
{
Runway simulator = new Runway();
}
}
当模拟运行时,除了退出函数之外,所有其他函数都会产生java.lang.NullPointerException错误。