【发布时间】:2018-05-21 01:11:39
【问题描述】:
发生的问题是错误是“缺少返回语句”;我该如何解决这个问题?代码的实际要点是将所有同伴的余额以及余额加在一起,然后看看是否足够放假。 我还声明了另一个名为
的字段变量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;
}
}
}
【问题讨论】:
-
如果
companions为空并且for循环甚至不会迭代一次怎么办?应该返回什么值?你是怎么保证的? -
顺便说一句,执行
return会将控制流移出您的方法,也会移出您的循环。由于您在两种if/else情况下都有return,这意味着您将在第一次迭代时退出哪种失败的循环目的。 -
Companions 是空的,直到通过创建对象并使用来自“member”类的指针来填充,该类最初存储数组列表伴随。应该返回的值是一个布尔值,因为它将“成员”余额与同伴相加,然后如果它超过假期成本,则返回 true。
标签: java for-loop if-statement arraylist bluej