简体   繁体   English

布尔值未使用构造函数初始化

[英]Boolean value not being initialized with Constructor

I created a simple system to create a Hamburger, where you can add ingredients and then calculate the price with the ingredients you picked, the base price is 5 without extra ingredients and for each ingredient you want more, I set it's value to true and I have a price counter that will add 1 more euro/dollar to the base price, the problem here is that the boolean fields aren't being initiated as false, even tho I set it in the class Constructor, I can't understand why. 我创建了一个简单的系统来创建汉堡包,您可以在其中添加成分,然后使用选择的成分计算价格,底价是5(不含额外的成分),对于每个想要的成分,我将其值设置为true有一个价格计数器,该计数器会将基本价格再增加1欧元/美元,这里的问题是布尔值字段不会被初始化为false,即使我在Constructor类中进行了设置,我也不明白为什么。

I tried setting the boolean fields to false because they're not in the parameters but they won't change, in the code I provided I want the price to be 5 because I haven't yet set any other ingredients so the the method price() should only return the base price I set(5) and not add any other ingredients, but that doesn't happen because all boolean fields are set to true meaning that they'll add 1 to each true value, so the returned price will be 9. This is probably really stupid to ask but I'm a total programming noob so I would thank if someone would explain why am I not obtaining the result I want. 我尝试将布尔字段设置为false,因为它们不在参数中,但它们不会更改,在我提供的代码中,我希望价格为5,因为我尚未设置任何其他成分,因此方法价格()应该只返回我设置的基本价格(5),而不添加任何其他成分,但是不会发生这种情况,因为所有布尔字段均设置为true,这意味着它们会将每个真实值加1,因此返回的价格将会是9。这可能真的很愚蠢,但是我是一个完全编程的菜鸟,所以我要感谢有人解释为什么我没有得到想要的结果。

public class Hamburger {
    private String bun;
    private String meat;
    private double price;
    private boolean letuce;
    private boolean tomato;
    private boolean bacon;
    private boolean sauce;

    public Hamburger(String bun, String meat) {
        this.bun = bun;
        this.meat = meat;
        this.price = 5;
        this.letuce = false;
        this.tomato = false;
        this.bacon = false;
        this.sauce = false;
    }

    public double price() {

        if(letuce = true)
            price+=1;
        if(tomato = true)
            price+=1;
        if(bacon = true)
            price+=1;
        if(sauce = true)
            price+=1;

        return price;           
    }

    public void setLetuce(boolean letuce) {
        this.letuce = letuce;
    }
    public void setTomato(boolean tomato) {
        this.tomato = tomato;
    }
    public void setBacon(boolean bacon) {
        this.bacon = bacon;
    }
    public void setSauce(boolean sauce) {
        this.sauce = sauce;
    }
}

I expect the output of 5 but the output is 9 我期望输出5但输出是9

Inside your if checks you use = instead of == . 在if检查中,您使用=而不是== The single = is an assignment and an assignment returns the value assigned, in this case true that's why your code returns 9 instead of 5 This will work as you expected: 单个=是一个分配,一个分配返回分配的值,在这种情况下为true ,这就是为什么您的代码返回9而不是5的原因。这将按预期工作:

public double price() {

    if(letuce == true)
        price+=1;
    if(tomato == true)
        price+=1;
    if(bacon == true)
        price+=1;
    if(sauce == true)
        price+=1;

    return price;           
}
letuce = true

是一个赋值,将letuce的值更改为true,并且本身具有true的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM