我无法正确更新总价。我正在输入book = 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));
}
}
答案 0 :(得分:2)
您可以在此处将项目价格的值添加到总费用中:
double price1 = Double.parseDouble(price);
totalprice += price1;
但是你在这里再做一次。
if (item.contains("music") == true)
{
double newprice = Double.parseDouble(price);
newprice = newprice * 1.10;
totalprice += newprice;
因此,您的总价格包括CD的原始价格(不含税)加到税价中。在某种程度上,你要重复计算CD的成本(但有一次是税,有一次没有)。
我建议将if (item.contains("music") == true)
代码块更改为
newprice = newprice * .10
你应该达到预期的效果。如果您进行此更改我强烈建议您更改变量的名称,以便它们表明目的(例如,在我更改之后,“newprice”变量实际上表示税,而不是更新价)。
答案 1 :(得分:2)
在此代码中:
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的价格(再次)加税。
答案 2 :(得分:0)
很多问题 - 这段代码无法编译。没有主要方法,对于初学者。
您将字符串与==进行比较;你应该使用equals。
真/假检查的正确习惯是使用布尔值。
其他很多方面都错了。
我认为这样的事情会更清晰。它具有正确计算总数的额外优势。
package misc;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
/**
* SalesTax description here
* @author Michael
* @link https://stackoverflow.com/questions/25351880/getting-the-total-of-user-input-doubles/25352033#25352033
* @since 8/17/2014 1:51 PM
*/
public class ShoppingCart {
private Map<String, Double> items;
private Map<String, Double> taxRates;
public ShoppingCart(Map<String, Double> taxRates) {
this.items = new HashMap<>();
this.taxRates = new HashMap<>();
if (taxRates != null) {
this.taxRates.putAll(taxRates);
}
}
public void addItem(String name, double price) {
double priceWithTax = (this.isTaxable(name) ? price*this.getTaxRate(name) : price);
this.items.put(name, priceWithTax);
}
public boolean isTaxable(String name) {
return this.taxRates.containsKey(name);
}
public double getTaxRate(String name) {
return (this.isTaxable(name) ? this.taxRates.get(name) : 1.0);
}
public double getTotal() {
double total = 0.0;
for (String name : items.keySet()) {
total += items.get(name);
}
return total;
}
public static void main(String[] args) {
// Create a Scanner
Scanner input = new Scanner(System.in);
Map<String, Double> taxRates = new HashMap<String, Double>() {{
put("music", 1.10);
put("imported", 1.05);
}};
ShoppingCart shoppingCart = new ShoppingCart(taxRates);
String line;
do {
System.out.print("Enter name, price or Q to quit: ");
line = input.nextLine();
String [] tokens = line.split(",");
if ((tokens.length > 1)) {
shoppingCart.addItem(tokens[0], Double.parseDouble(tokens[1]));
}
} while (!"Q".equalsIgnoreCase(line));
System.out.println(String.format("%.2f", shoppingCart.getTotal()));
}
}