Java折扣计算
我对编码还不熟悉,所以有疑问。 主要目标是获得物品的租赁价格(包括折扣)。 下面的代码工作正常,但只显示最小价格,忽略折扣。Java折扣计算,java,Java,我对编码还不熟悉,所以有疑问。 主要目标是获得物品的租赁价格(包括折扣)。 下面的代码工作正常,但只显示最小价格,忽略折扣。 如何计算折扣 在比较最低价格的代码块中,您可以只考虑每个项目的折扣 尝试替换: if (item[i].getItemPrice() < min) if(项[i].getItemPrice()
如何计算折扣 在比较最低价格的代码块中,您可以只考虑每个项目的折扣 尝试替换:
if (item[i].getItemPrice() < min)
if(项[i].getItemPrice()
有,
if(项[i].getItemPrice()*项[i].getItemDiscount()
希望有帮助!我已经有一段时间没有使用Java:)在您的
getLeastPriceItem
函数中有一点逻辑错误
问题#1:for(int i=1;iimport java.util.Scanner;
公共类查找价格{
私有静态双dis;
公共静态void main(字符串[]args)
{
扫描仪s=新的扫描仪(System.in);
System.out.println(“请输入项目名称:”);
字符串itmName=s.next();
System.out.println(“请输入项目编号:”);
int itmNo=s.nextInt();
System.out.println(“请输入项目费率:”);
双速率=s.nextDouble();
System.out.println(“请输入项目数量:”);
整数数量=s.nextInt();
双倍价格;
nPrice=费率*数量;//nPrice表示净价
如果(nPrice>=1000&&nPrice<2000)
dis=0.10*n价格;
其他的
如果(nPrice>=2000&&nPrice<3000)
dis=0.15*n价格;
其他的
如果(nPrice>=3000&&nPrice<4000)
dis=0.20*n价格;
其他的
如果(价格>=5000)
dis=0.25*n价格;
双tPrice=nPrice-dis;
双倍节省=nPrice-tPrice;
双百分比=(dis/nPrice)*100;
int eSavings=0;//额外节省…我将使用它进行更多练习。。
系统输出打印项次(“项目编号:“+itmNo”);
System.out.println(“项目名称为:“+itmName”);
System.out.println(“篮子的净价为:“+nPrice”);
System.out.println(“折扣百分比为:+dPercent+“%”);
System.out.println(“篮子上的折扣为:“+dis”);
System.out.println(“篮子上的额外节省:“+eSavings”);
System.out.println(“篮子上的总节省量为:“+节省量”);
System.out.println(“篮子总价为:“+tPrice”);
}
}
它仍然不起作用。仍然忽略折扣价格。使用建议的解决方案运行代码,但得到以下输出。0 1 2 3 4 Curr最低商品价格1.7976931348623157E308 Curr最低商品价格80.0 Curr最低商品价格70.0 Curr最低商品价格60.0 Curr最低商品价格50.0 leastitem price 100.0
@macboyme95-为我在那里得到的调试语句道歉。我已经更新了解决方案以删除它们,并使这些产品的定价更加明显,而不是对所有产品使用100.0值。还包括解决方案的示例输出。是的。它现在运行正确。只是最后一次查询。如果折扣率为We in percentage?然后你必须算出一个数学公式,它可能只是通常的乘除运算,并用它替换此行double afterDiscount=item[i].getItemPrice()-item[i].getItemDiscount();
。如果你标记(解决方案旁边的勾号),我将不胜感激作为对未来用户的回答。谢谢!如何显示最终价格(折扣后)
例如,在您的输出中,价格应该是400-390=10
您应该在回答中添加说明/注释。
if (item[i].getItemPrice() * item[i].getItemDiscount() < min)
public static void main(String[] args) {
Item[] item= new Item[5];
item[0] = new Item(1, "a", 100.0, 20.0);
item[1] = new Item(2, "b", 80.0, 30.0);
item[2] = new Item(3, "c", 300.0, 40.0);
item[3] = new Item(4, "d", 400.0, 390.0);
item[4] = new Item(5, "e", 500.0, 60.0);
Item cheapest = getLeastPriceItem(item);
System.out.println("Least item price: "+ cheapest.getItemPrice() + " with discount of: " + cheapest.getItemDiscount());
}
public static Item getLeastPriceItem(Item[] item)
{
double currMin = Double.MAX_VALUE;
Item cheapestItem = null;
for(int i=0;i<item.length;i++)
{
double afterDiscount = item[i].getItemPrice() - item[i].getItemDiscount();
if(afterDiscount < currMin)
{
currMin = afterDiscount;
cheapestItem = item[i];
}
}
return cheapestItem;
}
import java.util.Scanner;
public class FindingPrice {
private static double dis;
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.println("Please Enter the Item Name :");
String itmName = s.next();
System.out.println("Please Enter the Item No :");
int itmNo = s.nextInt();
System.out.println("Please Enter the Item rate :");
double rate = s.nextDouble();
System.out.println("Please Enter the Item Quantity :");
int quantity = s.nextInt();
double nPrice;
nPrice = rate * quantity; // nPrice means Net Price
if (nPrice >= 1000 && nPrice < 2000)
dis = 0.10 * nPrice;
else
if (nPrice >= 2000 && nPrice < 3000)
dis = 0.15 * nPrice;
else
if (nPrice >=3000 && nPrice < 4000)
dis = 0.20 * nPrice;
else
if (nPrice >= 5000)
dis = 0.25 * nPrice;
double tPrice = nPrice - dis;
double savings = nPrice - tPrice;
double dPercent = (dis/nPrice) * 100;
int eSavings = 0; // Extra Savings... I will use this for more practice..
System.out.println("Item Number is :" + itmNo);
System.out.println("Item Name is :" + itmName);
System.out.println("Net price of the basket is :" + nPrice);
System.out.println("Discount percentage is :" + dPercent + "%");
System.out.println("Discount on the Basket is :" + dis);
System.out.println("Extra savings on the basket :" + eSavings);
System.out.println("Total savings on the basket is :" + savings);
System.out.println("Total Price of Basket is :" + tPrice);
}
}