Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.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:55:error:';其他';没有';如果';_Java - Fatal编程技术网

java:55:error:';其他';没有';如果';

java:55:error:';其他';没有';如果';,java,Java,所以基本上我只是想让这段代码正常工作。当我在两个“if”语句下面加上“else”时,它就会运行。但是,当我将其移到上方时,会出现以下错误: Driver.java:55: error: 'else' without 'if' 我真的很感激能帮我解决这个问题,这样它就可以再次运行了 { synchronized(someObject) { someObject.wait(); System.out.printl

所以基本上我只是想让这段代码正常工作。当我在两个“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
语句的主体是多行的,则需要使用大括号
{}
。像这样^

if语句需要方括号始终使用大括号{}以避免混淆和错误。if语句不使用封闭方括号。因此,本例中的错误
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."); 
}