将字符串更改为双重赋值更新并分配回字符串

时间:2014-08-14 19:29:33

标签: java variable-assignment assignment-operator

我正在尝试将用户输入的字符串(价格)转换为double(price1)并将其分配给总价格变量。然后我将它转换回字符串并将其存储在地图中。我复制了目前为止编写的代码。

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;
        double totalwtax = 0;
        double totaltax;


        // 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;

            String price2 = String.valueOf(price1);
            shoppingcart.put(item, price2);

            cart.put(item, price);

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

                totalwtax +=newprice;

                String newprice2 = String.valueOf(newprice);
                shoppingcart.put(item, newprice2);
            }
            else if (item.contains("imported") == true)
            {
                double newprice = Double.parseDouble(price);
                newprice = newprice *1.05;

                totalwtax +=newprice;

                String newprice2 = String.valueOf(newprice);
                shoppingcart.put(item, newprice2);
            }
            else if(item.contains("bottle") == true)
            {
                double newprice = Double.parseDouble(price);
                newprice = newprice *1.10;

                totalwtax +=newprice;

                String newprice2 = String.valueOf(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);
            totaltax = totalprice - totalwtax;

        } while(Character.toUpperCase(done) == 'Y');
       System.out.println("Your cart contains the following at items with tax" + shoppingcart);
       System.out.println("Total Tax: " + totalwtax);

    }
}

测试用例

问题是,如果我输入12.49,14.99,0.85并且在14.99上有10%的税,则我不会得到税款总额。这三项税总额应该是29.83:

Please enter an item.standard
Please enter the price for standard: 12.49
Would you like to continue to add items? (Type Y) for Yes and (Type N) for No.y
Please enter an item.music
Please enter the price for music: 14.99
Would you like to continue to add items? (Type Y) for Yes and (Type N) for No.y
Please enter an item.discount
Please enter the price for discount: 0.85
Would you like to continue to add items? (Type Y) for Yes and (Type N) for No.n
Your cart contains the following at items with tax{music=16.489, standard=12.49, discount=0.85}
Total Tax: 16.489

为什么这段代码会产生错误的答案?

2 个答案:

答案 0 :(得分:2)

为了最大限度地减少您必须进行的转化次数,您可以使用扫描器nextDouble()方法而不是nextLine()将价格直接读作双精度而不是字符串。这将在价格中读取为双倍,因此您不必转换它。此外,将价格作为double存储在地图中似乎更有意义(即拥有Map<String, Double>而不是Map<String, String>,这样您就不会必须进行任何转换。

答案 1 :(得分:1)

一个问题是没有正确计算totalwtax,因为任何零税率的项目都不包括在总数中。将else子句更改为:

        else
        {
            double newprice = Double.parseDouble(price);
            totalwtax +=newprice;

            shoppingcart.put(item, price);
        }

为您的测试用例生成此输出:

Please enter an item.standard
Please enter the price for standard: 12.49
Would you like to continue to add items? (Type Y) for Yes and (Type N) for No.y
Please enter an item.music
Please enter the price for music: 14.99
Would you like to continue to add items? (Type Y) for Yes and (Type N) for No.y
Please enter an item.discount
Please enter the price for discount: 0.85
Would you like to continue to add items? (Type Y) for Yes and (Type N) for No.n
Your cart contains the following at items with tax{music=16.489, standard=12.49, discount=0.85}
Total Tax: 29.829

这仍然与预期答案完全相同,因为预期答案假定计算中有一些舍入。您可能需要自己舍入值,或者使用Money类可能是合适的,这样才能正确计算所有分数。