【发布时间】:2015-10-17 06:01:40
【问题描述】:
public BigDecimal calculateTotal() {
BigDecimal percent = BigDecimal.valueOf(0.9);
int i = 0;
BigDecimal price = BigDecimal.valueOf(0.0);
while(!myOrders.isEmpty()){
if (!myOrders.get(i).getItem().isBulk() && myMembership == true){
price = price.add(myOrders.get(i).calculateOrderTotal().multiply(percent));
myOrders.remove(i);
}
else{
price = price.add(myOrders.get(i).calculateOrderTotal());
myOrders.remove(i);
}
}
//WHY IS THIS UNREACHABLE?!
return price.setScale(2, RoundingMode.HALF_EVEN);
}
我知道 return 语句之后的任何代码都是无法访问的代码,但我唯一的 return 语句是无法访问的,我不知道为什么。 while 循环是这样的,因为我正在抓住稻草,我知道它可能不会做我想要它做的事情。 myOrders 是一个 ArrayList。
【问题讨论】:
-
这里的 myOrders 变量到底是什么? (数据类型)
-
编译器是说它无法访问还是您只是无法访问它?
-
编译器说它无法访问。我正在使用日食。
-
我看不出有什么原因。也许尝试在eclipse中清理项目。
-
如果解决了你的问题,请将对应的答案标记为solution,这样以后的读者可以很容易地找到解决方案。
标签: java return unreachable-code