1

我是 Java 新手,我们正在根据不同的重量和距离进行运费计算器。我们正在练习 if-else 语句,但我遇到了一个我无法弄清楚的问题。当我计算不同的重量和距离时,我得到了错误的答案。我认为这与我的数学有关,我似乎无法让程序添加下一个距离费用,因为它没有使用剩余部分。请帮助我理解这一点。

public class ShippingCharges {

private double weight;
private double miles;


public ShippingCharges (double w, double m)
{
    weight = w;
    miles = m;
}


public double getShippingCharges()
{
    double charges;
    if (weight <= 2.0)
    { charges = (1.10 * miles / 500);
    }
    else if ((weight > 2.0) && (weight <= 6.0))
    {
        charges = (2.20 * (miles  / 500 ));
    }
    else if ((weight > 6.0) && (weight <=10.0))
    {
        charges = (3.70 * (miles / 500 )); 
    }
    else 
    {
        charges = (4.80 * miles  / 500);
    }
    return charges;

    }
}
4

1 回答 1

3

从你所说的我理解问题是你没有四舍五入英里/ 500结果,试试这个:

public double getShippingCharges()
{
    double charges;
    if (weight <= 2.0)
    { charges = (1.10 * Math.ceil(miles / 500));
    }
    else if ((weight > 2.0) && (weight <= 6.0))
    {
        charges = (2.20 * Math.ceil(miles  / 500 ));
    }
    else if ((weight > 6.0) && (weight <=10.0))
    {
        charges = (3.70 * Math.ceil(miles / 500 )); 
    }
    else 
    {
        charges = (4.80 * Math.ceil(miles  / 500));
    }
    return charges;

    }
于 2013-03-17T21:13:00.040 回答