英文:
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
<= 2000 5% WALL CLOCK
2001 – 5000 10% BAG
5001 – 10000 15% ELECTRIC IRON
>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("Input the total cost of the product...");
double cost = in.nextDouble();
if(cost<=0)
{
System.out.println("Invalid Price");
}
else if(cost<=2000)
{
System.out.println("Total cost of the product "+cost);
System.out.println("Discount is 5%");
double dis = 5/100*cost;
double finalcost = dis+cost;
System.out.println("Discounted price is "+dis);
System.out.println("Final amount to be paid is "+finalcost);
System.out.println("Gift recieved is a Wall Clock");
}
else if(cost>=2001 && cost<=5000)
{
System.out.println("Total cost of the product "+cost);
System.out.println("Discount is 10%");
double dis = 10/100*cost;
double finalcost = dis+cost;
System.out.println("Discounted price is "+dis);
System.out.println("Final amount to be paid is "+finalcost);
System.out.println("Gift recieved is a Bag");
}
else if(cost>=5001 && cost<=10000)
{
System.out.println("Total cost of the product "+cost);
System.out.println("Discount is 15%");
double dis = 15/100*cost;
double finalcost = dis+cost;
System.out.println("Discounted price is "+dis);
System.out.println("Final amount to be paid is "+finalcost);
System.out.println("Gift recieved is an Electric Iron");
}
else if(cost>10000)
{
System.out.println("Total cost of the product "+cost);
System.out.println("Discount is 20%");
double dis = 20/100*cost;
double finalcost = dis+cost;
System.out.println("Discounted price is "+dis);
System.out.println("Final amount to be paid is "+finalcost);
System.out.println("Gift recieved is a Wrist Watch");
}
else
{
System.out.println("Invalid Price");
}
}
}
</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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论