Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么不是';我的代码不能计算正确的总数吗?_Java_Arrays_Loops_Methods - Fatal编程技术网

Java 为什么不是';我的代码不能计算正确的总数吗?

Java 为什么不是';我的代码不能计算正确的总数吗?,java,arrays,loops,methods,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 nam

我很难找出总数。每次我计算运行总数时,它都会计算错误,并给出错误的答案。我不知道是什么。我无法判断它是否与我在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));
        }
    }
}
导入java.text.DecimalFormat;
导入javax.swing.JOptionPane;
公屋{
公共静态void main(字符串[]args){
String name=JOptionPane.showInputDialog(null,“您的名字是什么?”);
greetCustomer(姓名);
双倍总价=订单(名称);
计算最终价格(总价);
}
公共静态无效greetCustomer(字符串名称){
//问候顾客
showMessageDialog(null,“Hello”+name+”,欢迎来到一杯Java!);
}
公共静态双takeOrder(字符串名称){//此方法返回
字符串[]食物={“咖啡”、“百吉饼”、“茶”、“松饼”};
双[]价格={3.99,1.99,2.99,4.99};
双倍总价=0;
DecimalFormat dec=新的DecimalFormat(#.00”);
对于(int index=0;index
当您这样做时

double salestax= totalPrice + 0.07; //7% salestax
finalprice=salestax + 0.02;
这意味着你要加税7美分,而不是7%。要增加7%,您需要将原价乘以1.07或100%+7%=107%

double salestax= totalPrice * 1.07; // add 7% salestax
当你这样做的时候

double salestax= totalPrice + 0.07; //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];
    } 
}
//首先获取输入
字符串输入=JOptionPane.showInputDialog(null//
“您有兴趣从哪些项目购买”
+“我们的菜单:咖啡、百吉饼、茶还是松饼?”;

对于(int index=0;index您是否可以提供较少的信息?您期望什么输入?您期望什么输出?您看到什么输出?调试时发生了什么?使用附加的调试器/使用的打印语句浏览代码时发生了什么?您还尝试了什么?问题似乎出在哪里?谢谢!我更改了它,但它仍然给我一个错误的计算。我不知道方法调用是否被正确调用:/能否举例说明您输入的内容以及您希望输出的内容?我更新了它。我的输入将是3个项目。这些项目是1个松饼、1个茶和1个百吉饼,我想在其中用餐。我仍然得到0.00美元的折扣layed?我认为它与takeOrder方法中的double totalprice=0有关。这就像是覆盖了其他所有内容,忽略了我的for循环和if语句。因此,当它返回totalprice时,它返回0。totalprice应该是9.97美元。你明白我的意思吗?所有这些加在一起(包括销售税和进餐税)它应该显示$10.88作为总数。我希望我解释得足够好:/@ASH在您的代码中,您假设食物将按照它在数组中出现的相同顺序输入。如果是这样,您就什么也不做。相反,您可以按任何顺序读取任意数量的单词,并且仍然可以计算。要做到这一点,您需要一个嵌套循环或使用Map数据结构在这里,使用调试器调试代码将有助于理解代码的用途和原因。