简单java程序中的折扣错误

时间:2014-11-26 18:37:59

标签: java

因此,在我的程序中,它运行并正确显示,但我的折扣无法正确计算,因此错误。

以下是问题:

  

我们的Java Coffee Web业务决定为批量购买者提供折扣。折扣基于下表:

Order Volume   Discount
>=25  bags     5% of total price
>=50  bags    10% of total price
>=100 bags    15% of total price
>=150 bags    20% of total price  
>=200 bags    25% of total price
>=300 bags    30% of total price
  

每袋咖啡的价格为1.75英镑。编写一个接受订购行李数量的应用程序,并按以下方式打印出订单的总成本:

Number of Bags Ordered=  173 - £302.75
Discount:    20% - £60.55   
Your total charge is:   £242.20
£242.20

我的程序运行,但为相同的输入提供以下内容:

Please enter your required number of Bags :173
Number of Bags Ordered: 302.75
Discount:0.25

Your total charge is:302.50

代码如下所示:

Scanner scanner = new Scanner(System.in);

DecimalFormat df = new DecimalFormat("00.00");

System.out.print("Please enter your required number of Bags :");
numberOfBags = scanner.nextInt();

{
    if ( numberOfBags > 299) {
        discount = 0.30;  
    }
    else if (numberOfBags < 199) {
        discount = 0.25;
    }
    else if ( numberOfBags < 149) {
        discount = 0.20;
    }
    else if ( numberOfBags < 99) {
        discount = 0.15;
    }
    else if ( numberOfBags < 49) {
        discount = 0.10;
    }
    else if ( numberOfBags < 24) {
        discount = 0.05;
    }
    if (numberOfBags<=24)
    {
        discount = .00;
    }

    totalDiscount =numberOfBags * pricePerBag *discount;

    totalCost = numberOfBags * pricePerBag;
    finalTotal =totalCost - discount;

    System.out.println("Number of Bags Ordered: " + numberOfBags *pricePerBag);
    System.out.println ("Discount:"      + discount );
    System.out.print("\r"+"Your total charge is:" +df.format(finalTotal));
}

3 个答案:

答案 0 :(得分:0)

不是Java问题,而是逻辑问题。您没有打印想要的部件。

您只是在{<1}}变量中打印:

discount

您似乎希望将折扣百分比应用于总价格(折扣之前)。毕竟,这是百分比的折扣金额。因此,请打印正确的变量:System.out.println ("Discount:" + discount );

对于totalDiscount,从finalTotal减去totalDiscount

答案 1 :(得分:0)

totalDiscount =numberOfBags * pricePerBag *discount;
totalCost = numberOfBags * pricePerBag;
        finalTotal =totalCost - discount;

除非它是拼写错误,否则我认为您只需要将 totalCost 中的折扣更改为 totalDiscount

答案 2 :(得分:0)

非常感谢你们......妻子最终把我排好了..

我有我的&gt; =  错误的方法..我设置了&lt; =

现在......输出是:

请输入您所需的行李数量:173 订购的行李数量:302.75 折扣:20.0%£60.55

您的总费用是:242.20