这是我的代码。当我使用bluej进行编译时,它会突出显示最后一个}并且说缺少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+ " seconds remaining on the clock");
jobsFinished = true;
} else {
System.out.println("JobQueue is empty");
}
}
答案 0 :(得分: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;
}
答案 1 :(得分: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;
}
答案 2 :(得分: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;
}
答案 3 :(得分:0)
你只是忘了放 返回工作完成; 在System.out.print行结束之后。 当你这样做时,它应该成功运行和编译,我确信这是因为我今天犯了同样的错误:P
祝你好运。-Keelen
答案 4 :(得分:0)
更改您的功能定义/原型...使用此public void runAJob()
代替您使用的内容...
将boolean
替换为原型中的void
..然后尝试
答案 5 :(得分: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;
}