Java 为什么';我的附加构造函数没有被调用吗?

Java 为什么';我的附加构造函数没有被调用吗?,java,Java,这是我正在进行的项目的说明 现在我只上基本的汉堡课 问题是:为什么我的additionalChoice构造函数没有被调用?它不会增加总价格,因此我认为它不会被调用。有什么问题吗 注:如果你有任何关于更好地接近这个项目和更好地编写这个程序的建议,请写下来,我将非常感谢 我的代码: class Burger { private String name; private String meatType; private String breadType; priv

这是我正在进行的项目的说明

现在我只上基本的汉堡课

问题是:为什么我的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();


    }

}

您有一个不必要的
while
循环。除去

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


代码从不输入while

只需删除此行及其右括号:

while (addition1=="done") {

我现在不知道为什么会有,但是你永远不会进入
switch
语句,除非你像参数一样传递
done

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

addition1.equals("done")
甚至

"done".equals(addition1)
哪一个更好,因为如果
addition1
null
,它不会抛出
NullPointerException

其次,您的while循环没有多大意义。只有当您传递
“done”
参数时,您才会进入它的块,这似乎不符合逻辑。但是,如果您在(!“done”.equals(addition1))转换为“只要
addition1
“done”
时将条件否定为
,则在调用例如

basicBurger.additionalChoices("tomato");

在这种情况下,你似乎不需要任何while循环。至少你给我们看的那一个是完全失败的。

答案和我的不一样吗?你说这是我正在进行的项目的说明,这是你的期望。你需要坚持到目前为止你所做的,你的问题是什么环比较不正确。您应该使用
.equals()时使用
=
@Ravi我想清楚我在问什么,为什么这么多仇恨?????@Mehrs。哈哈,首先,这里不能接受用张贴图片而不是打字,其次,简单地说这是我正在进行的项目的说明。相反,总结一下,你在找什么,你尝试了什么,什么阻碍了你。你张贴图片的方式我们的问题似乎是您发布了一个需要满足的要求。@Mehrs。请阅读这些内容,并记住,您的帖子可能会在以后的某个时间点对其他人有所帮助,因此您的问题和标题不应针对您的项目,否则其他人将很难找到。