所以我有这两个类,分别称为装运和保险,一类计算运输价格,另一类添加保险。装运逻辑工作正常,但由于某种原因,保险类中的if语句不起作用,我不知道发生了什么。保险成本的输出始终为2.45。为什么这样做呢?

货运等级:

  package theshipment;

  public class Shipment extends Main {
      protected double weight = Double.parseDouble(savedArgs[1]);
      protected double shippingCost;
      protected double methodCost;
      protected String method = savedArgs[2];

      public void calculateShippingCost(){

        if (weight<=10||weight>=1){
            if (method.equalsIgnoreCase("T"))
                methodCost = weight * 3.00;
            else if (method.equalsIgnoreCase("A"))
                methodCost = weight * 4.00;
            else if (method.equalsIgnoreCase("M"))
                methodCost = weight * 2.00;
            else{}
        }else if (weight<=20||weight>=10.1){
            if (method.equalsIgnoreCase("T"))
                methodCost = weight * 2.45;
            else if (method.equalsIgnoreCase("A"))
                methodCost = weight * 3.00;
            else if (method.equalsIgnoreCase("M"))
                methodCost = weight * 1.75;
            else{}
        }else if (weight>20){
            if (method.equalsIgnoreCase("T"))
                methodCost = weight * 1.95;
            else if (method.equalsIgnoreCase("A"))
                methodCost = weight * 2.50;
            else if (method.equalsIgnoreCase("M"))
                methodCost = weight * 1.55;
            else{}
        }else{}

    }
}


和保险类:

package theshipment;

public class Insurance extends Shipment{
    private void calculateInsurance(){
        if (methodCost<=10.0||methodCost>=0.0)
            shippingCost = methodCost + 2.45;
        else if(methodCost<=30.0||methodCost>=10.1)
            shippingCost = methodCost + 3.95;
        else if(methodCost>=30.01)
            shippingCost = methodCost + 5.55;
        else{}
    }

    public void run(){
        calculateShippingCost();
        calculateInsurance();
    }

    public String displayOrder(){
        return ("Method cost: " + methodCost + " " + "Insurance cost: " +
               (shippingCost-methodCost) + " Total shipping cost: " + shippingCost);
    }

}

最佳答案

methodCost<=10.0||methodCost>=0.0表示methodCost大于0或小于10时为true,

我相信您希望范围不是所有数字都将其更改为methodCost<=10.0 && methodCost>=0.0

关于java - 如果语句逻辑不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28587808/

10-13 00:48