0

这是我的代码。当我使用 bluej 编译时,它会突出显示最后一个 } 并说缺少 return 语句。我不能放 void 因为它是一个布尔值。有任何想法吗?

 public boolean runAJob() {
     boolean jobsFinished = false;
     Job firstJob = getCurrentJob();
     String jobName = firstJob.getName();
     int jobDuration = firstJob.getDuration();
     if (!myJob.isEmpty()&& jobDuration > 0 && jobDuration <= myTotalDuration) { 
         myTotalDuration -= jobDuration;
         myFinishedJobs.add(myJob.get(0));
         myJob.remove(0);
         System.out.println("'"+jobName+"'" +" is completed. "+ myTotalDuration+ " seconds remaining on the clock");
         jobsFinished = true;
    } else {
          System.out.println("JobQueue is empty");
    }
} 
4

6 回答 6

1

在 Java 中,与在类 C 语言中一样,需要基于方法/函数的返回类型的 return 语句。

在你的情况下:

public boolean runAJob() { ... }

要求返回一个布尔值因此,当您尝试编译代码并且方法体中没有 return 语句时,编译器会报错。

因此,您要做的是确定您希望返回的信息(在这种情况下为布尔值)。正如其他人指出的那样,您应该返回jobsFinished 因为这是一个布尔类型,您希望在方法调用时确定其值

 public boolean runAJob()
    {
        boolean jobsFinished = false;
        Job firstJob = getCurrentJob();
        String jobName = firstJob.getName();
        int jobDuration = firstJob.getDuration();
        if (!myJob.isEmpty()&& jobDuration > 0 && jobDuration <= myTotalDuration)
        { 
             myTotalDuration -= jobDuration;
             myFinishedJobs.add(myJob.get(0));
             myJob.remove(0);
             System.out.println("'"+jobName+"'" +" is completed. "+ myTotalDuration+ " seconds remaining on the clock");
             jobsFinished = true;
             return jobsFinished; //this is one possible place

        }
        else
        {
            System.out.println("JobQueue is empty");
        }
        //here is another possible place where you could have put the return
        //return jobsFinished;
    }
于 2012-10-14T04:46:59.193 回答
0

return 语句必须始终是方法中的最后一条语句

public boolean runAJob()
    {
        boolean jobsFinished = false;

        Job firstJob = getCurrentJob();

        String jobName = firstJob.getName();

        int jobDuration = firstJob.getDuration();

        if (!myJob.isEmpty()&& jobDuration > 0 && jobDuration <= myTotalDuration)

        { 

             myTotalDuration -= jobDuration;

             myFinishedJobs.add(myJob.get(0));

             myJob.remove(0);

             System.out.println("'"+jobName+"'" +" is completed. "+ myTotalDuration+ " 

时钟上剩余的秒数");

             jobsFinished = true;

        }

        else

        {

            System.out.println("JobQueue is empty");

        }

        return jobsFinished;
    } 
于 2013-03-24T21:04:51.163 回答
0

更改您的函数定义/原型...使用它public void runAJob()而不是您使用的...

在原型中替换booleanvoid..然后尝试

于 2014-05-18T16:36:13.033 回答
0

你只是忘了把返回的工作完成;在 System.out.print 行结束之后。当你这样做时,它应该可以成功运行和编译,我确信这一点,因为我今天犯了同样的错误:P

祝你好运。

-基伦

于 2013-12-13T22:02:38.510 回答
0

您已指定boolean返回类型,但您的函数不返回任何值。

jobsFinished变量是多余的,您的else分支也是如此:

public boolean runAJob() {
    Job firstJob = getCurrentJob();    
    int jobDuration = firstJob.getDuration();

    if (!myJob.isEmpty() && jobDuration > 0 && jobDuration <= myTotalDuration) { 
        String jobName = firstJob.getName();
        myTotalDuration -= jobDuration;
        myFinishedJobs.add(myJob.get(0));
        myJob.remove(0);
        System.out.println("'"+jobName+"'" +" is completed. "+ myTotalDuration+ " seconds remaining on the clock");
        return true;
    } 

    System.out.println("JobQueue is empty");
    return false;
}

如果满足条件,则该函数return无论如何都会在最后两个语句之前,因此else没有必要。我还移动了分支String jobName = firstJob.getName();内部,if因为您没有在其他情况下使用它。

如果您更喜欢在函数末尾使用boolean变量return,那么您可以这样做:

public boolean runAJob() {
    Job firstJob = getCurrentJob();    
    int jobDuration = firstJob.getDuration();
    boolean jobsFinished = !myJob.isEmpty() && jobDuration > 0 && jobDuration <= myTotalDuration;
    if (jobsFinished) { 
        String jobName = firstJob.getName();
        myTotalDuration -= jobDuration;
        myFinishedJobs.add(myJob.get(0));
        myJob.remove(0);
        System.out.println("'"+jobName+"'" +" is completed. "+ myTotalDuration+ " seconds remaining on the clock");            
    } 
    else {
        System.out.println("JobQueue is empty");
    }   
    return jobsFinished;
}
于 2014-05-18T17:00:10.173 回答
0

你应该简单地返回jobsFinished。

要清楚:

public boolean runAJob()
        {
            boolean jobsFinished = false;
            Job firstJob = getCurrentJob();
            String jobName = firstJob.getName();
            int jobDuration = firstJob.getDuration();
            if (!myJob.isEmpty()&& jobDuration > 0 && jobDuration <= myTotalDuration)
            { 
                 myTotalDuration -= jobDuration;
                 myFinishedJobs.add(myJob.get(0));
                 myJob.remove(0);
                 System.out.println("'"+jobName+"'" +" is completed. "+ myTotalDuration+ " seconds remaining on the clock");
                 jobsFinished = true;
            }
            else
            {
                System.out.println("JobQueue is empty");
            }
            return jobsFinished;
        } 
于 2012-10-14T04:43:09.647 回答