gpt4 book ai didi

java - 使用户输入的总数加倍

转载 作者:行者123 更新时间:2023-12-01 18:27:59 24 4
gpt4 key购买 nike

我无法正确更新总价。我输入书籍 = 12.49 音乐 cd = 14.99 和巧克力棒 = 0.85。发生的情况是 14.99 * .10 正在添加和巧克力棒。它正在删除第一个值 12.49。

public class SalesTax {
public static void main(String[] args)
{
// Input items for shopping cart
HashMap<String, String> cart = new HashMap<String, String>();
HashMap<String, String> shoppingcart = new HashMap<String, String>();

// Create a Scanner
Scanner input = new Scanner(System.in);

// variables
char done;
//boolean goods;
double totalprice = 0.00;
double taxprice;

// Pick items for list.
do
{
System.out.print("Please enter an item.");
String item = input.nextLine();

System.out.print("Please enter the price for "+ item + ": ");
String price = input.nextLine();

double price1 = Double.parseDouble(price);
totalprice += price1;

//System.out.println(String.format("%.2f",totalprice));
String price2 = String.valueOf(price1);
cart.put(item, price2);

//determine if item will have additional tax
if (item.contains("music"))
{
double newprice = Double.parseDouble(price);
taxprice = newprice * .10;

totalprice = (newprice + taxprice);

//System.out.println(String.format("%.2f",totalprice));
String newprice2 = String.valueOf(String.format("%.2f", totalprice));
shoppingcart.put(item,newprice2);
}
else if (item.contains("imported"))
{
double newprice = Double.parseDouble(price);
newprice = newprice * .05;

totalprice += newprice;
//System.out.println(String.format("%.2f",totalprice));
String newprice2 = String.valueOf(String.format("%.2f", newprice));
shoppingcart.put(item, newprice2);
}
else if(item.contains("bottle"))
{
double newprice = Double.parseDouble(price);
newprice = newprice * .10;

totalprice += newprice;
System.out.println(String.format("%.2f",totalprice));
String newprice2 = String.valueOf(String.format("%.2f", newprice));
shoppingcart.put(item, newprice2);
}
else
{
shoppingcart.put(item, price);
}


System.out.print("Would you like to continue to add items? (Type Y) for Yes and (Type N) for No.");
done = input.nextLine().charAt(0);
} while(Character.toUpperCase(done) == 'Y');

System.out.println("Your cart contains the following at items with tax" + shoppingcart); //+ String.format("%.2f", totalprice));
System.out.println(String.format("%.2f",totalprice));
}

}

最佳答案

在此代码中:

        if (item.contains("music") == true)
{
double newprice = Double.parseDouble(price);
newprice = newprice * 1.10;

totalprice += newprice;
//System.out.println(String.format("%.2f",totalprice));
String newprice2 = String.valueOf(String.format("%.2f", newprice));
shoppingcart.put(item,newprice2);
}

值1.10应该是0.10,因为您已经在上面添加了音乐CD的价格,现在只需添加税即可。使用值 1.10,您将添加 CD 的价格(再次)加上税费。

关于java - 使用户输入的总数加倍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25351880/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com