为什么我的additionalChoices构造函数不会被调用?

时间:2018-01-14 05:41:32

标签: java

这是我正在进行的项目的指示。

instructions

现在我只在基本的汉堡包课上工作。

问题变成:为什么我的additionalChoice构造函数不会被调用?它不会增加总价格,所以我认为它不会被调用。可能是什么问题?

注意:如果您对更好地接近这个项目有任何建议并且更好地编写这个程序,请写下来我非常感谢。

我的代码:

class Burger {

    private String name;
    private String meatType;
    private String breadType;

    private double totalPrice = 0;
    private String addition1;

    public Burger(String name, String meatType, String breadType) {
        System.out.println("Your name is " + name);
        this.name = name;
        this.meatType = meatType;
        meatChoice(meatType);
        this.breadType = breadType;
        breadChoice(breadType);
    }


    public double getTotalPrice() {
        System.out.println("You need to pay : " +  totalPrice);
        return totalPrice;
    }

    public double meatChoice( String meatType) {
            if (meatType == "chicken") {
                System.out.println("You have selected Chicken");
                totalPrice += 3;
            } else if (meatType == "beef") {
                System.out.println("You have selected Beef");
                totalPrice += 3;
            } else if (meatType == "ham") {
                System.out.println("You have selected Ham");
                totalPrice += 3;
            } else {
                System.out.println("We do not have the meat selected, please try again");
                totalPrice += 0;
            }        return totalPrice;

    }

    public double breadChoice(String breadType) {
        if (breadType == "french") {
            System.out.println("You have selected french bread");
            totalPrice += 2;
        } else if (breadType == "persian") {
            System.out.println("You have selected persian bread");
            totalPrice += 2;
        }
        return totalPrice;
    }

    public double additionalChoices(String addition1){
        while (addition1=="done") {
            switch (addition1) {
                case "tomato":
                    System.out.println("You have selected Tomato for your topping. If you are done type in 'done'");
                    totalPrice += 2;
                    break;
                case "lettuce":
                    System.out.println("You have selected lettuce for your topping.If you are done type in 'done'");
                    totalPrice += 2;
                    break;
                case "onion":
                    System.out.println("You have selected onion for your topping.If you are done type in 'done'");
                    totalPrice += 2;
                    break;
                case "olive":
                    System.out.println("You have selected olive for your topping.If you are done type in 'done'");
                    totalPrice += 2;
                    break;
                case "done":
                    System.out.println("Thank you for your oder");
                default:
                    System.out.println("Invalid input");
            }

        } return totalPrice;


}
}


public class Main {

    public static void main(String[] args) {
        Burger basicBurger = new Burger("mehr","beef","french");
        basicBurger.additionalChoices("tomato");
        basicBurger.additionalChoices("olive");
        basicBurger.getTotalPrice();


    }

}

3 个答案:

答案 0 :(得分:0)

您有一个不必要的while(changeDue>=0.25){ changeDue=changeDue-0.25; quarter=quarter+1; } while(changeDue>=0.10){ changeDue=changeDue-0.10; dime=dime+1; } 循环。删除

while

以及相应的 while (addition1=="done") {

代码永远不会进入

答案 1 :(得分:0)

只需删除此行并关闭括号:

FeatureManagerProvider

我现在不知道为什么会这样,但你永远不会进入while (addition1=="done") { 陈述,除非你通过switch这样的论证。

答案 2 :(得分:0)

你的additionalChoices(String addition1)方法中的while循环被破坏了。首先,您不能将Java中的字符串与==运算符进行比较,而是使用.equals()方法,例如

addition1.equals("done")

甚至

"done".equals(addition1)

哪个更好,因为如果NullPointerExceptionaddition1,它就不会抛出null

其次,你的while循环没有多大意义。只有在传递"done"参数时才会进入其块内,这似乎不是逻辑。但是如果你否定条件while (!"done".equals(addition1))转换为"只要addition1不是"done",那么你会在调用eg时遇到无限循环。

basicBurger.additionalChoices("tomato");

在这种情况下,您似乎并不需要任何while循环。至少不是你向我们展示的那个,它完全被打破了。