一家软件公司出售零售价为99美元的套餐。如果订购的包裹数量为:
,则应用数量折扣Quantity Discount
10 - 19 20%
20 - 49 30%
50 - 99 40%
100 or more 50%
创建一个应用程序SoftwareSales,提示用户输入订购的软件包数量,计算购买成本并显示结果。 该应用程序可以是基于控制台或GUI。
我该如何处理?
我知道我需要这样的东西吗?
if(score < 10) {
discount = 0.0
} else if(score < 20){
discount = 0.1
}
// other cases go here
任何人都希望至少为我这样的程序提供框架/骨架?
答案 0 :(得分:0)
import javax.swing.JOptionPane;
int userInput = Integer.parseInt(JOptionPane.showInputDialog("Enter no. of packages: "));
double cost = 99;
double finalCost;
if(userInput>=10 && userInput<20){
finalCost= cost - ((cost/100)*20)
} else if(userInput>=20 && userInput<50){
finalCost= cost - ((cost/100)*30)
} else if( etc.)
这是您需要的基本变量,您需要做的就是填写每个折扣的条件。如果我给你所有代码,你就不会学到太多东西,希望它有所帮助!
答案 1 :(得分:0)
你必须使用这样的东西:
if(score >= 10 && <= 19)
{
discount = 0.20;
}
else if(score >= 20 && <= 49)
{
discount = 0.30;
}
And so on....
答案 2 :(得分:0)
我要用一张桌子:
private static class Discount {
private final int fromQuantity;
private final double percent;
public Discount(int fromQuantity, double percent) {
this.fromQuantity = fromQuantity;
this.percent = percent;
}
}
public static final Discount[] DISCOUNT_TABLE = {
new Discount(10, 20),
new Discount(20, 30),
new Discount(50, 40),
new Discount(100, 50)
};
要获取数量的折扣百分比:
public static double discountForQuantity(int quantity) {
int i = 0;
do {
--i;
} while (i >= 0 && DISCOUNT_TABLE[i].fromQuantity > quantity);
return i < 0 ? 0 : DISCOUNT_TABLE[i].percent;
}
要计算一定数量产品的价格:
public static double priceForQuantity(int quantity, double basePrice) {
return quantity*basePrice*(1-(discountForQuantity(quantity)/100));
}