Java中的BigDecimal错误



所有与价格/有关的内容都有错误

导入java.math.BigDecimal;

公共类产品{

    // Fields
    String name;
    String description;
    BigDecimal price = new BigDecimal(3.0);
    int quantity;
    String barcode;
    String image;
    static int count;

    // Constructors
    public Product()
    {
        name = "";
        description = "";
        price = 0;       
        quantity = 0;
        barcode = "";
        image = "";
    }
    public Product(String n, String d, double p, int q, String b, String i)
    {
        name = n;
        description = d;
        price = p;
        quantity = q;
        barcode = b;
        image = i;
    }

    // Get/Set methods
    // description getter and setter
    public String getDescription()
    {
        return description;
    }
    public void setDescription(String d)
    {
        this.description = d;
    }

    // name getter and setter
    public String getName()
    {
        return name;
    }
    public void setName(String d)
    {
        this.name = d;
    }

    // price getter and setter
    public double getPrice()
    {
        return price;
    }
    public void setPrice(double d)
    {
        this.price = d;
    }

    // quantity getter and setter
    public int getQuantity()
    {
        return quantity;
    }
    public void setQuantity(int d)
    {
        this.quantity = d;
    }

    // barcode getter and setter
    public String getBarcode()
    {
        return barcode;
    }
    public void setBarcode(String d)
    {
        this.barcode = d;
    }

    // image getter and setter
    public String getImage()
    {
        return image;
    }
    public void setImage(String d)
    {
        this.image = d;
    }

}

我的问题是为什么我所有的价格部件都有错误。我需要它是一个大小数,但我该如何纠正错误?

好吧,你的构造函数给p的值,也就是你的bigDecimal,一个双精度。你需要为它提供一个BigDecimal:)所以在arguments中,为"p"提供它,而不是"double"。

Java使用了一种名为"静态检查"的东西,这意味着即使在你打字的时候,它也可以判断出,例如,如果你有一个变量被声明为BigDecimal,并且你试图将其设置为另一种类型的变量,那么就错了。如果你把鼠标悬停在变量下面的红色曲线上,它会给你非常有用的信息!

最新更新