gpt4 book ai didi

java - 驱动程序.java :55: error: 'else' without 'if'

转载 作者:行者123 更新时间:2023-11-30 03:13:00 27 4
gpt4 key购买 nike

所以基本上我只是想让这段代码正常工作。当我在两个“if”语句下面有“else”时,它就会运行。但是,当我将其移至上方时,会出现以下错误:

Driver.java:55: error: 'else' without 'if'

我非常感谢您帮助解决此问题,以便它再次运行。

     {
synchronized(someObject)
{
someObject.wait();
System.out.println(ID + " has been notified.");

Customer customer = null;

if(Dispatcher.customerRequiresPickUp() && (customer = Dispatcher.queue.poll()) != null && (rideCount >= workFinished))

System.out.println("Customer has been picked up by driver #" + ID);

try
{
Thread.sleep(rn.nextInt(5000) + 1000);
System.out.println("Driver #" + ID + " has dropped off " + customer.getName() + " at " + customer.getEndLocation() + ".");
}
catch(InterruptedException e)
{
}

else
System.out.println("Driver #" + ID + " will continue to wait for a customer.");

if(rideCount >= workFinished)
onDuty(false);
}
}

最佳答案

if(Dispatcher.customerRequiresPickUp() && (customer = Dispatcher.queue.poll()) != null && (rideCount >= workFinished))
{

System.out.println("Customer has been picked up by driver #" + ID);

try
{
Thread.sleep(rn.nextInt(5000) + 1000);
System.out.println("Driver #" + ID + " has dropped off " + customer.getName() + " at " + customer.getEndLocation() + ".");
}
catch(InterruptedException e)
{
}
}
else
{
System.out.println("Driver #" + ID + " will continue to wait for a customer.");
}

如果 if 语句的主体超过一行,则需要大括号 {}。就像这样^。

关于java - 驱动程序.java :55: error: 'else' without 'if' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33248496/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com