为什么这个在布尔值中一直返回 false?

huangapple go评论89阅读模式
英文:

Why this is always returning false for boolean?

问题

以下是翻译好的内容:

/*{Nescafe, Minas}
------------------------产品------------------------
咖啡{ 非研磨咖啡 产品{name=Nescafe, price=780.89, expireDate=20/06/2019}
咖啡{ 非研磨咖啡 产品{name=Minas, price=360.19, expireDate=02/12/2018}
Nescafe, 843.36
Minas, 389.01

这是代码:
*/

public class Product {

    protected String name;
    protected double price;
    protected String expireDate;

    public Product(String name, double price, String expireDate) {
        this.name = name;
        this.price = price;
        this.expireDate = expireDate;
    }

    public double pricePlusTaxes() {
        return price;
    }

    public String getname() {
        return name;
    }

    public void setIme(String ime) {
        this.name = name;
    }

    public double getCena() {
        return price;
    }

    public void setCena(double cena) {
        this.price = price;
    }

    public String getRokTrajanja() {
        return expireDate;
    }

    public void setRokTrajanja(String rokTrajanja) {
        this.expireDate = rokTrajanja;
    }

    @Override
    public String toString() {
        return "Product{" + "name=" + name + ", price=" + price + ", expireDate=" + expireDate + '}';
    }

}

public class Coffee extends Product {
    public int codeForIsGroundCoffee;
    public boolean groundCoffee;

    public Coffee(int codeForIsGroundCoffee, String name, double price, String expireDate) {
        super(name, price, expireDate);
        this.codeForIsGroundCoffee = codeForIsGroundCoffee;
    }

    @Override
    public double pricePlusTaxes() {
        return price * 1.08;
    }

    public boolean isgroundCoffee() {
        return groundCoffee;
    }

    public void setGroungCoffee(int codeForIsGroundCoffee) {

        if (codeForIsGroundCoffee == 1) {
            groundCoffee = true;
        } else {
            groundCoffee = false;
        }

    }

    @Override
    public String toString() {
        if (groundCoffee) {
            return "咖啡{ 研磨咖啡 " + super.toString();
        }
        return "咖啡{ 非研磨咖啡 " + super.toString();
    }

}

public class Runner2 {

    private static List<Product> list = new ArrayList<>();

    public static void addProduct(Product p) {
        list.add(p);
    }

    public static void DeleteProduct(Product p) {
        list.remove(p);
    }

    public static void PrintProductName(List<Product> list) {
        System.out.print("{");
        for (int i = 0; i < list.size() - 1; i++) {
            System.out.print(list.get(i).getname() + ", ");
        }
        System.out.println(list.get(list.size() - 1).name + "}");
    }

    public static void printListOfProducts(List<Product> lista) {
        System.out.println("------------------------产品------------------------");
        for (Product p : lista) {
            System.out.println(p);
        }
    }

    public static void printProductPrice(List<Product> lista) {
        for (Product p : lista) {

            System.out.printf("%s, %.2f\n", p.name, p.pricePlusTaxes());
        }
    }

    public static void main(String[] args) {

        Coffee nes = new Coffee(1, "Nescafe", 780.89, "20/06/2019");
        Coffee minas = new Coffee(2, "Minas", 360.19, "02/12/2018");

        addProduct(nes);
        addProduct(minas);

        PrintProductName(list);
        printListOfProducts(list);
        printProductPrice(list);
    }

}

注意:此处的翻译只是对你提供的代码进行了翻译,不包括额外的解释或回答。

英文:

Why this is always returning me "not Ground Coffee" or false for public boolean groundCoffee?
Thanks in front...

This result:

/*{Nescafe, Minas}
------------------------PRODUCTS------------------------
Coffe{ not groundCoffee Product{name=Nescafe, price=780.89, expireDate=20/06/2019}
Coffe{ not groundCoffee Product{name=Minas, price=360.19, expireDate=02/12/2018}
Nescafe, 843.36
Minas, 389.01
This is code:
*/
public class Product {
protected String name;
protected double price;
protected String expireDate;
public Product(String name, double price, String expireDate) {
this.name = name;
this.price = price;
this.expireDate = expireDate;
}
public double pricePlusTaxes(){
return price;
}
public String getname() {
return name;
}
public void setIme(String ime) {
this.name = name;
}
public double getCena() {
return price;
}
public void setCena(double cena) {
this.price = price;
}
public String getRokTrajanja() {
return expireDate;
}
public void setRokTrajanja(String rokTrajanja) {
this.expireDate = rokTrajanja;
}
@Override
public String toString() {
return &quot;Product{&quot; + &quot;name=&quot; + name + &quot;, price=&quot; + price + &quot;, expireDate=&quot; + expireDate + &#39;}&#39;;
}
}
public class Coffee extends Product {
public int codeForIsGroundCoffee;
public boolean groundCoffee;
public Coffee(int codeForIsGroundCoffee,String name, double price, String expireDate) {
super(name, price, expireDate);
this.codeForIsGroundCoffee = codeForIsGroundCoffee;
}
@Override
public double pricePlusTaxes(){
return price * 1.08;
}
public boolean isgroundCoffee() {
return groundCoffee;
}
public void setGroungCoffee(int codeForIsGroundCoffee) {
if (codeForIsGroundCoffee == 1){
groundCoffee = true;
}else{
groundCoffee = false;
}
}
@Override
public String toString() {
if(groundCoffee){
return &quot;Coffe{&quot; + &quot; ground Coffee &quot; + super.toString();
}
return &quot;Coffe{&quot; + &quot; not groundCoffee &quot; + super.toString();
}
}
public class Runner2 {
private static List&lt;Product&gt; list = new ArrayList&lt;&gt;();
public static void addProduct(Product p) {
list.add(p);
}
public static void DeleteProduct(Product p) {
list.remove(p);
}
public static void PrintProductName(List&lt;Product&gt; list) {
System.out.print(&quot;{&quot;);
for (int i = 0; i &lt; list.size() - 1; i++) {
System.out.print(list.get(i).getname()+ &quot;, &quot;);
}
System.out.println(list.get(list.size() - 1).name + &quot;}&quot;);
}
public static void printListOfProducts(List&lt;Product&gt; lista){
System.out.println(&quot;------------------------PROIZVODI------------------------&quot;);
for(Product p : lista){
System.out.println(p);
}
}
public static void printProductPrice(List&lt;Product&gt; lista){
for(Product p : lista){
System.out.printf(&quot;%s, %.2f\n&quot;, p.name, p.pricePlusTaxes());
}
}
public static void main(String[] args) {
Coffee nes = new Coffee(1, &quot;Nescafe&quot;, 780.89, &quot;20/06/2019&quot;);
Coffee minas = new Coffee(2, &quot;Minas&quot;, 360.19, &quot;02/12/2018&quot;);
addProduct(nes);
addProduct(minas);
PrintProductName(list);
printListOfProducts(list);
printProductPrice(list);
}
}
</details>
# 答案1
**得分**: 1
问题在于你没有调用setGroundCoffee。即使你在构造函数中提供了1,你仍需要调用setGroundCoffee。成员变量`codeForIsGroundCoffee`实际上并没有被使用,因为在调用setGroundCoffee时,它会隐藏该值。
移除codeForIsGroundCoffee,只使用一个布尔值。然后在构造函数和设置方法中简单地设置为true/false。
```java
public Coffee(boolean groundCoffee, String name, double price, String expireDate) {
super(name, price, expireDate);
this.groundCoffee = groundCoffee;
}
英文:

The problem is that you're not calling setGroundCoffee. Even if you provide the constructor with 1 you need to call setGroundCoffee. The member codeForIsGroundCoffee is not actually being used since setGroundCoffee hides that value when you call it.

Remove codeForIsGroundCoffee and only use a boolean. Then simply set true/false in the constructor and the setter method.

       public Coffee(boolean groundCoffee, String name, double price, String expireDate) {
            super(name, price, expireDate);
            this.groundCoffee = groundCoffee;
        }

答案2

得分: 0

以下是您提供的代码的翻译部分:

所以我必须承认尽管您并没有完全回答我的问题但您确实帮助了我解决了争论关于咖啡类的代码如下所示

public class Coffee extends Product {
    public int codeForIsGroundCoffee;
    public int value;
    boolean groundCoffee;

    public Coffee(int value, String name, double price, String expireDate) {
        super(name, price, expireDate);
        this.value = value;
        this.groundCoffee = (value == 1);
    }

    @Override
    public double pricePlusTaxes() {
        return price * 1.08;
    }

    @Override
    public String toString() {
        if (groundCoffee) {
            return "咖啡是地咖啡 " + super.toString();
        }
        return "咖啡不是地咖啡 " + super.toString();
    }
}

或者是这样的:

public class Coffee extends Product {
    public int codeForIsGroundCoffee;
    public int value;
    boolean groundCoffee;

    public Coffee(int value, String name, double price, String expireDate) {
        super(name, price, expireDate);
        this.value = value;
        this.groundCoffee = gc(value);
    }

    public boolean gc(int value) {
        if (value == 1) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public double pricePlusTaxes() {
        return price * 1.08;
    }

    @Override
    public String toString() {
        if (groundCoffee) {
            return "咖啡是地咖啡 " + super.toString();
        }
        return "咖啡不是地咖啡 " + super.toString();
    }
}
英文:

So, I got to admit that you have helped me after all dispute the fact that you haven't had answer my question completely. The code for class coffee goes like this.

public class Coffee extends Product {
public int codeForIsGroundCoffee;
public int value;
boolean groundCoffee ; 
public Coffee(int value,String name, double price, String expireDate) {
super(name, price, expireDate);
this.value = value;
this.groundCoffee = (value==1);
}
@Override
public double pricePlusTaxes(){
return price * 1.08;
}
@Override
public String toString() {
if(groundCoffee){
return &quot;Coffe is {&quot; + &quot; ground Coffee &quot; + super.toString();
}
return &quot;Coffe{&quot; + &quot; not groundCoffee &quot; + super.toString();
}
}

Or something like this :

public class Coffee extends Product {
public int codeForIsGroundCoffee;
public int value;
boolean groundCoffee  ; 
public Coffee(int value,String name, double price, String expireDate) {
super(name, price, expireDate);
this.value = value;
this.groundCoffee = gc(value);
}
public boolean gc(int value){
if (value == 1){
return true;
}else
return false;
}
@Override
public double pricePlusTaxes(){
return price * 1.08;
}
@Override
public String toString() {
if(groundCoffee){
return &quot;Coffe is {&quot; + &quot; ground Coffee &quot; + super.toString();
}
return &quot;Coffe{&quot; + &quot; not groundCoffee &quot; + super.toString();
}
}

huangapple
  • 本文由 发表于 2020年4月5日 05:10:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/61034838.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定