这个问题在这里已有答案:
发生的问题是错误是“缺少return语句”;我该如何解决这个问题?代码的实际要点是将所有伴侣的余额和余额相加,然后查看它是否足以满足假期。我还声明了另一个字段变量
private int every1nzBalance;
public boolean checkMoney(Holiday holiday)
{
// For each friend i.e in the list
for(Friend friend:companions)
{
if(friend.getMoney()+ balance>=holiday.getPrice())
{
System.out.println("You including your friends have sufficient funds to pay for the"+holiday+" holiday.");
return true;
}
else
{
System.out.println("Please ensure that you and your friends have sufficient amount of money for the holiday");
return false;
}
}
}
这很简单。如果for
没有任何迭代(比如数组为空),那么它根本不会执行。您最后需要一个默认的return语句:
private int every1nzBalance;
public boolean checkMoney(Holiday holiday) {
if(companions == null) return false; //also default return
for(Friend friend : companions)
{
if(friend.getMoney()+ balance>=holiday.getPrice())
{
System.out.println("You including your friends have sufficient funds to pay for the"+holiday+" holiday.");
return true;
}
else
{
System.out.println("Please ensure that you and your friends have sufficient amount of money for the holiday");
return false;
}
}
return false; //default return
}