gpt4 book ai didi

java - 使用嵌套 if 语句进行计算

转载 作者:行者123 更新时间:2023-12-01 22:45:51 29 4
gpt4 key购买 nike

我正在开发一个嵌套的程序,如果我的数学是正确的,唯一的事情是正确的输出没有打印。我不知道应该将输出行放在第一个 if、嵌套 if 或两行中的哪里?我测试的价格是一瓶进口香水,价格为 27.99。输出应该是 32.19,我得到的是 34.99。

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;
double ftax;

// 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"))
{
price1 = Double.parseDouble(price);
taxprice = price1 * .10;

totalprice = (totalprice + taxprice);

if (item.contains("imported"))
{
price1 = Double.parseDouble(price);
ftax = price1 * .05;

totalprice = (totalprice + taxprice + ftax);
}

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

totalprice = (price1 + taxprice);

if (item.contains("imported"))
{
price1 = Double.parseDouble(price);
ftax = price1 * .05;

totalprice = (totalprice + taxprice + ftax);
}

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

totalprice = (totalprice + taxprice);

if (item.contains("imported"))
{
price1 = Double.parseDouble(price);
ftax = price1 * .05;

totalprice = (totalprice + taxprice + ftax);
}

//System.out.println(String.format("%.2f",totalprice));
String newprice2 = String.valueOf(String.format("%.2f", totalprice));
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));
}
}

最佳答案

尝试使用bigDecimal而不是float或double。

事实上,如果你尝试这样做:

double value = 10*0.09;
System.out.println(value);

你有:

0.8999999999999999

使用此方法:

BigDecimal value1 = new BigDecimal("10");
BigDecimal value2 = new BigDecimal("0.09");
BigDecimal value = value1.multiply(value2);
System.out.println(value);

关于java - 使用嵌套 if 语句进行计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25385632/

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