缺少退货单吗? [英] Missing Return Statement?

查看:115
本文介绍了缺少退货单吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

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

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

在您的情况下:

public boolean runAJob() { ... }

要求返回布尔值.这样,当您尝试编译代码时,并且在方法主体中没有return语句时,编译器会抱怨.

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

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

这篇关于缺少退货单吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆