运费计算器未命中计算

时间:2016-02-27 02:20:19

标签: java if-statement java.util.scanner

作业具有以下费率: 每运送500英里的包装重量 2磅或更少$ 1.10 超过2磅但不超过6磅2.20美元 超过6磅但不超过10磅3.70美元 超过10磅$ 3.80

每500英里的运费不按比例分配。例如,如果一个2磅的包装运送502英里,则费用为2.20美元。编写一个程序,要求用户输入包裹的重量,然后显示运费。

我的问题是我得到了错误的答案。这是我到目前为止所得到的:

import java.util.Scanner;
public class ShippingCharges
{
public static void main (String [] args)
{
    double mDrive, rMiles, wPound;

    Scanner keyboard = new Scanner (System.in);

    System.out.print ("Enter Weight of Package: ");
    wPound = keyboard.nextDouble();
    System.out.println("");

    System.out.print ("Enter Miles Driven: ");
    mDrive = keyboard.nextDouble();
    System.out.println("");

    rMiles = mDrive / 500;

    if (wPound <2)
    {
        System.out.println ("You will be charged "+ "$" + Math.ceil(rMiles)*1.10);
    }

    if (wPound >=2 && wPound <6)
    {
        System.out.println ("You will be charged "+ "$" + Math.ceil(rMiles)*2.20);
    }

    if (wPound >=6 && wPound <10)
    {
        System.out.println ("You will be charged "+ "$" + Math.ceil(rMiles)*3.70);
    }

    if (wPound >= 10)
    {
        System.out.println ("You will be charged "+ "$" + Math.ceil(rMiles)*3.80);
    }

}
}

按照这个例子,程序应该是502/500 * 2.2,这是2.2,程序显示为4.4。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

你的if语句是罪魁祸首。按照您提供的说明,声明应如下所示

if (wPound<=2) {
    System.out.println ("You will be charged "+ "$" + Math.ceil(rMiles)*1.10);
}
else if(wPound<=6) {
    System.out.println ("You will be charged "+ "$" + Math.ceil(rMiles)*2.20);
}
else if (wPound<=10) {
    System.out.println ("You will be charged "+ "$" + Math.ceil(rMiles)*3.70);
}
else {
    System.out.println ("You will be charged "+ "$" + Math.ceil(rMiles)*3.80);
}