这个 if 语句阶梯无法计算折扣。

huangapple go评论80阅读模式
英文:

This if statement ladder can't figure out the discount

问题

这是我制作的一个程序,除了不显示折扣价和最终成本外,一切正常运行。
我正在使用if else阶梯和扫描仪(这行是不必要的,但是堆栈溢出不允许我发布问题,如果我不写足够的文字)。
如果您可以帮助我,我会非常感谢您。
以下是代码:

/**一个店主决定根据物品的总成本向客户提供折扣和保证礼品:
总成本   折扣  礼品
≤ 2000       5%      墙上时钟
2001 – 5000   10%     包
5001 – 10000   15%    电熨斗
>10000         20%    手表
编写一个程序来输入总成本。 计算折扣。 显示总成本,获得的折扣,
要支付的最终金额以及客户收到的礼物。*/
import java.util.*;
class P1
{
    public static void main(String a[])
    {
        Scanner in = new Scanner(System.in);
        System.out.println("输入产品的总成本...");
        double cost = in.nextDouble();
        if(cost<=0)
        {
            System.out.println("无效价格");
        }
        else if(cost<=2000)
        {
            System.out.println("产品的总成本:"+cost);
            System.out.println("折扣为5%");
            double dis = 5/100*cost;
            double finalcost = dis+cost;
            System.out.println("折后价格为:"+dis);
            System.out.println("要支付的最终金额为:"+finalcost);
            System.out.println("收到的礼物是墙上时钟");
        }
        else if(cost>=2001 && cost<=5000)
        {
            System.out.println("产品的总成本:"+cost);
            System.out.println("折扣为10%");
            double dis = 10/100*cost;
            double finalcost = dis+cost;
            System.out.println("折后价格为:"+dis);
            System.out.println("要支付的最终金额为:"+finalcost);
            System.out.println("收到的礼物是一个包");
        }
        else if(cost>=5001 && cost<=10000)
        {
            System.out.println("产品的总成本:"+cost);
            System.out.println("折扣为15%");
            double dis = 15/100*cost;
            double finalcost = dis+cost;
            System.out.println("折后价格为:"+dis);
            System.out.println("要支付的最终金额为:"+finalcost);
            System.out.println("收到的礼物是电熨斗");
        }
        else if(cost>10000)
        {
            System.out.println("产品的总成本:"+cost);
            System.out.println("折扣为20%");
            double dis = 20/100*cost;
            double finalcost = dis+cost;
            System.out.println("折后价格为:"+dis);
            System.out.println("要支付的最终金额为:"+finalcost);
            System.out.println("收到的礼物是手表");
        }
        else
        {
            System.out.println("无效价格");
        }
    }
}
英文:

This is a program that I made and everything works except it doesn't display the discounted price and thus the final cost too.
I am using if else ladder and scanner (This line was unnecessary but stacks overflow won't let me post my question if I don't write enough text)
I would be really thankful if you can help me thanks.
Here's the code :

/**A Shopkeeper has decided to give out discount and an assured gifts to his customer on the basis of total cost of the item purchased:
TOTAL COST   DISCOUNT GIFT
&lt;= 2000       5%      WALL CLOCK
2001 – 5000   10%     BAG
5001 – 10000   15%    ELECTRIC IRON
&gt;10000         20%    WRIST WATCH
Write a program to input the total cost. Compute discount. Display the total cost, discount obtained,
final amount to be paid and the gift received by the customer.*/
import java.util.*;
class P1
{
public static void main(String a[])
{
Scanner in = new Scanner(System.in);
System.out.println(&quot;Input the total cost of the product...&quot;);
double cost = in.nextDouble();
if(cost&lt;=0)
{
System.out.println(&quot;Invalid Price&quot;);
}
else if(cost&lt;=2000)
{
System.out.println(&quot;Total cost of the product &quot;+cost);
System.out.println(&quot;Discount is 5%&quot;);
double dis = 5/100*cost;
double finalcost = dis+cost;
System.out.println(&quot;Discounted price is &quot;+dis);
System.out.println(&quot;Final amount to be paid is &quot;+finalcost);
System.out.println(&quot;Gift recieved is a Wall Clock&quot;);
}
else if(cost&gt;=2001 &amp;&amp; cost&lt;=5000)
{
System.out.println(&quot;Total cost of the product &quot;+cost);
System.out.println(&quot;Discount is 10%&quot;);
double dis = 10/100*cost;
double finalcost = dis+cost;
System.out.println(&quot;Discounted price is &quot;+dis);
System.out.println(&quot;Final amount to be paid is &quot;+finalcost);
System.out.println(&quot;Gift recieved is a Bag&quot;);
}
else if(cost&gt;=5001 &amp;&amp; cost&lt;=10000)
{
System.out.println(&quot;Total cost of the product &quot;+cost);
System.out.println(&quot;Discount is 15%&quot;);
double dis = 15/100*cost;
double finalcost = dis+cost;
System.out.println(&quot;Discounted price is &quot;+dis);
System.out.println(&quot;Final amount to be paid is &quot;+finalcost);
System.out.println(&quot;Gift recieved is an Electric Iron&quot;);
}
else if(cost&gt;10000)
{
System.out.println(&quot;Total cost of the product &quot;+cost);
System.out.println(&quot;Discount is 20%&quot;);
double dis = 20/100*cost;
double finalcost = dis+cost;
System.out.println(&quot;Discounted price is &quot;+dis);
System.out.println(&quot;Final amount to be paid is &quot;+finalcost);
System.out.println(&quot;Gift recieved is a Wrist Watch&quot;);
}
else
{
System.out.println(&quot;Invalid Price&quot;);
}
}
}
</details>
# 答案1
**得分**: 1
你需要将整数除法转换为双精度数。例如:
double dis = (double) 5 / 100 * cost;
<details>
<summary>英文:</summary>
You need to cast the integer division to double. For example:
double dis = (double) 5 / 100 * cost;
</details>

huangapple
  • 本文由 发表于 2020年9月6日 23:21:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63765757.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定