簡體   English   中英

為什么我的代碼沒有計算出正確的總數?

[英]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];
    } 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM