我已经用我所做的更改编辑了下面的代码。这是我得到的输出。
请输入商品。进口香水瓶 请输入进口香水瓶价格:47.50 您想继续添加项目吗? (Y 型)表示 Yes,(N 型)表示 No.n 您的购物车包含以下含税商品{进口盒巧克力=10.00,进口瓶香水=67.50} 67.50
一盒巧克力应该是10.50,进口瓶应该是54.65。我使用调试并发现我使用的总价格变量为 10.50,但看起来又变回来了。另外,不要读取具有
的循环 if (item.contains("imported") && item.contains("bottle"))
上面写着
else if (item.contains("imported"))
首先。我的完整代码如下。
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"))
{
price1 = Double.parseDouble(price);
taxprice = price1 * .10;
totalprice = (totalprice + taxprice);
//System.out.println(String.format("%.2f",totalprice));
String newprice2 = String.valueOf(String.format("%.2f", price1 * 1.10));
shoppingcart.put(item,newprice2);
}
else if (item.contains("imported"))
{
price1 = Double.parseDouble(price);
taxprice = price1 * .05;
totalprice = (totalprice + taxprice);
//System.out.println(String.format("%.2f",totalprice));
String newprice2 = String.valueOf(String.format("%.2f", totalprice));
shoppingcart.put(item,newprice2);
}
if (item.contains("imported") && item.contains("bottle"))
{
price1 = Double.parseDouble(price);
taxprice = price1 * (.05 + .10);
totalprice = (totalprice + taxprice);
//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);
//System.out.println(String.format("%.2f",totalprice));
String newprice2 = String.valueOf(String.format("%.2f", price1 * 1.10));
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));
}
}
最佳答案
使用&&
运算符。它称为逻辑 AND 运算符。
//if item contains both "imported" and "bottle"
if (item.contains("imported") && item.contains("bottle")){
//code
}
关于java - 如果满足多个条件,如何使用 If 语句计算数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25368396/