Missing Return Statement?

2019-03-05 03:16发布

This is my code. when I compile using bluej it highlights the last } and says missing return statement. I cant put void because it is a boolean. ANy ideas?

 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");
    }
} 

标签: bluej
6条回答
劫难
2楼-- · 2019-03-05 03:52

You should simply return jobsFinished.

To be clear:

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;
        } 
查看更多
神经病院院长
3楼-- · 2019-03-05 03:58

In Java, as in C-like languages, require a return statement based on the return type of the method / function.

In your case:

public boolean runAJob() { ... }

Requires that a boolean be returned. As such, when you try to compile the code, and there isn't a return statement in your method body, the compiler will complain.

So, what you have to do is determine what information you wish to return(a boolean in this case). As others have pointed out, you should return jobsFinished since that is a boolean type whose value you wish to determine upon the method call.

 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;
    }
查看更多
倾城 Initia
4楼-- · 2019-03-05 03:58

return Statement must be always the last statement in the method

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;
    } 
查看更多
对你真心纯属浪费
5楼-- · 2019-03-05 04:08

You just forgot to put return jobsFinished; after the end of the System.out.print line. When you do, it should run and compile successfully, which I am sure of because I made the same mistake today :P

Good luck.

-Keelen

查看更多
Anthone
6楼-- · 2019-03-05 04:08

You have specified a boolean return type but your function doesn't return any value.

The jobsFinished variable is redundant, as is your else branch:

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;
}

If the conditions are satisfied, the function will return before the last two statements anyway, so an else isn't necessary. I also moved the String jobName = firstJob.getName(); inside the if branch because you're not using it otherwise.

If you prefer to use a boolean variable, and return it at the end of the function, then you could do:

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;
}
查看更多
我只想做你的唯一
7楼-- · 2019-03-05 04:09

Change your function definition/prototype... Use this public void runAJob() instead of what you have used...

Replace boolean with void in the prototype.. And then try

查看更多
登录 后发表回答