简体   繁体   English

我的退货声明无法到达

[英]My Return Statement is Unreachable

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

I know that anything after a return statement is unreachable code, but my only return statement is unreachable and I can't figure out why. 我知道return语句之后的所有内容都是无法访问的代码,但是我唯一的return语句是不可访问的,而且我不知道为什么。 The while loop is the way it is because I'm grasping at straws, I'm aware that it probably won't do what I want it to do. while循环就是这样,因为我正在抓住稻草,我知道它可能不会做我想做的事情。 myOrders is an ArrayList. myOrders是一个ArrayList。

EDIT : Since OP said it is an ArrayList , my answer no longer applies. 编辑 :由于OP表示这是一个ArrayList ,我的答案不再适用。

You never update your index i . 您永远不会更新索引i This should work: 这应该工作:

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);
        }
        i++;    // <-- You were missing this
    }
    // Not unreachable anymore :)
    return price.setScale(2, RoundingMode.HALF_EVEN);
}

Your variable i is never incremented. 您的变量i永远不会增加。 Depending on what type of Collection myOrders is, removing the 0th element each time may not shift the elements in the collection, and myOrders will never be empty. 取决于集合myOrders类型, myOrders删除第0个元素可能不会移动集合中的元素,并且myOrders永远不会为空。

There is nothing in the posted code to explain the error. 发布的代码中没有任何内容可以解释该错误。 Since you said your IDE is Eclipse, I suggest to clean the project. 既然您说您的IDE是Eclipse,我建议清理该项目。 Also, make sure to fix all other errors before looking at this one. 另外,在查看此错误之前,请确保已修复所有其他错误。 This error doesn't make sense, I suspect you have other compiler errors in your project, which somehow cause this as a strange side effect. 该错误没有意义,我怀疑您的项目中还有其他编译器错误,这些错误会以某种方式引起这种奇怪的副作用。 After you fix everything else, this one should naturally disappear. 修复所有其他问题后,该选项自然会消失。

Btw, to see clearer, here's a cleaned up version of the same code, doing exactly the same thing: 顺便说一句,为了更清楚地看到,这是相同代码的清理版本,其功能完全相同:

BigDecimal percent = BigDecimal.valueOf(0.9);
BigDecimal price = BigDecimal.ZERO;

while (!myOrders.isEmpty()) {
    Order first = myOrders.get(0);
    BigDecimal subtotal = first.calculateOrderTotal();
    if (!first.getItem().isBulk() && myMembership) {
        subtotal = subtotal.multiply(percent);
    }
    price = price.add(subtotal);
    myOrders.remove(0);
}
return price.setScale(2, RoundingMode.HALF_EVEN);

蚀食解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM