如何解决这个if语句梯子无法计算出折扣
这是我制作的程序,除不显示折扣价和最终成本外,其他所有功能均正常。 我正在使用if else梯形图和扫描仪(此行是不必要的,但是如果我写的文字不足,则堆栈溢出不会让我发表问题) 如果您能帮助我,我将非常感谢。 这是代码:
/**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");
}
}
}
解决方法
您需要将整数除法转换为两倍。例如:
double dis = (double) 5 / 100 * cost;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。