为什么我的代码不能计算出正确的总数?

时间:2015-04-09 23:58:55

标签: java arrays loops methods

我无法找到正在运行的总数。每次我计算运行总计时,都会误算并给出错误的答案。我不确定它是什么。我无法判断它是否与调用我在main中执行的方法,takeOrder中的if语句或两者都没有关系。

import java.text.DecimalFormat;

import javax.swing.JOptionPane;

public class MyCoffeeHouse {
    public static void main(String[] args) {
        String name = JOptionPane.showInputDialog(null, "What is your name?");
        greetCustomer(name);
        double totalPrice = takeOrder(name);
        calculateFinalPrice(totalPrice);
    }

    public static void greetCustomer(String name) {
        // greet customer
        JOptionPane.showMessageDialog(null, "Hello " + name + ", Welcome to A Cup of Java!");
    }

    public static double takeOrder(String name) { // this method returns
        String[] food = {"coffee", "bagel", "tea", "muffin"};
        double[] price = {3.99, 1.99, 2.99, 4.99};
        double totalprice = 0;
        DecimalFormat dec = new DecimalFormat("#.00");

        for (int index = 0; index < food.length; index++) {
            JOptionPane.showMessageDialog(null, "Our menu offers: " + food[index] + "(s) which is " + "$"
                + price[index]);
        }

        int numItems =
            Integer.parseInt(JOptionPane.showInputDialog(name + ", How many items are "
                + "you interested in purchasing?"));

        // running total
        for (int index = 0; index < numItems; index++) {
            String input =
                JOptionPane.showInputDialog(null, "Which items are you interested in purchasing from "
                    + "our menu: coffee, bagel, tea, or muffin?");

            if (input.equalsIgnoreCase(food[index])) {
                totalprice += price[index];
            }
        }
        return totalprice;
    }

    public static void calculateFinalPrice(double totalPrice) {
        double salestax = (totalPrice * 0.07) + totalPrice; // 7% salestax
        double finalprice;
        DecimalFormat dec = new DecimalFormat("#.00");

        int input =
            JOptionPane.showConfirmDialog(null, "Would you like to dine in?", "Confirm",
                JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);

        if (input == JOptionPane.YES_OPTION) {
            finalprice = totalPrice + (salestax * 0.02);
            JOptionPane.showMessageDialog(null, "The final price is $" + finalprice);
        } else {
            DecimalFormat dec = new DecimalFormat("#.00");
            JOptionPane.showMessageDialog(null, "The final price is $" + dec.format(salestax));
        }
    }
}

2 个答案:

答案 0 :(得分:2)

当你这样做时

double salestax= totalPrice + 0.07; //7% salestax

这意味着你要加7美分的税,而不是7%。要增加7%,您需要将原始价格乘以1.07或100%+ 7%= 107%

double salestax= totalPrice * 1.07; // add 7% salestax

当你这样做时

finalprice=salestax + 0.02;

你加2美分。注意:此时你可以添加另外2%但是增加7%而另外2%增加9%不同于1.07 * 1.02&gt; 1.09。

答案 1 :(得分:0)

在循环测试之前获取所选的食物项目。

// First get the input
String input=JOptionPane.showInputDialog(null,//
    "Which items are you interested in purchasing from "
    + "our menu: coffee, bagel, tea, or muffin?");
for(int index=0;index<numItems;index++) {
    // now loop all of the items, and check what was picked.
    if(input.equalsIgnoreCase(food[index])) {
         totalprice+=price[index];
    } 
}