Missing Return Statement?

后端 未结 6 1152
半阙折子戏
半阙折子戏 2021-01-25 20:52

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?

 p         


        
6条回答
  •  抹茶落季
    2021-01-25 21:13

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

提交回复
热议问题