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

Why isn't my code calculating the correct total?

我无法找出 运行 总数。每次我计算 运行 总数时,它都会计算错误并给出错误的答案。我不确定那是什么。我不知道它是否与我在 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));
        }
    }
}

当你这样做时

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 > 1.09.

在循环测试价格之前获取所选的食物。

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