【问题标题】:Getting the total of user input doubles使用户输入的总数翻倍
【发布时间】:2014-10-10 16:36:36
【问题描述】:

我无法正确更新总价。我正在输入 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));
}

}

【问题讨论】:

  • 作为建议,在您放置if (item.contains("something") == true) 的每个条件下,您都可以简化它:if (item.contains("something")),因为 contains 函数返回一个布尔值。

标签: java math assignment-operator


【解决方案1】:

您在此处将商品价格的价值添加到总成本中:

       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”变量实际上是税的指示,而不是更新的价格)。

【讨论】:

  • 有道理,所以有什么建议不要让它计数两次?
  • 我编辑了对 if 块的建议更改的答案
  • 我现在遇到的问题是它只会保存两个数字。我正在输入书籍 = 12.49,音乐 cd = 14.99,巧克力棒 = 0.85。我总共得到 17.34。它不会添加 12.49。
  • 您能发布任何其他相关代码吗?你在哪里定义“购物车”
  • 使用totalprice = (totalprice + taxprice); 而不是totalprice = (newprice + taxprice);
【解决方案2】:

很多问题 - 此代码无法编译。对于初学者,没有 main 方法。

您将字符串与 == 进行比较;你应该使用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()));
    }
}

【讨论】:

  • 实际上在 OP 的代码中没有进行 == 字符串比较。乍一看似乎是这样,但他实际上比较了字符或做了一个== true 位。
【解决方案3】:

在这段代码中:

        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 的价格加上税。

【讨论】:

    猜你喜欢
    • 2021-10-18
    • 2011-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-05
    • 1970-01-01
    相关资源
    最近更新 更多