Java 中的大小数错误

Error With Big Decimal in Java

我不断收到

The method add(BigDecimal) in the type BigDecimal is not applicable for the arguments (pay)"

以下代码出错。

作为参考,我已将 pay class 保存在一个单独的文件中,我也在其中导入 BigDecimal

你们中有人愿意指出我lacking/misunderstanding在哪里吗?我试图找到解决方案,但找不到任何东西。

import java.util.Scanner;
import java.math.BigDecimal;

class SalesPreInt { 
    public static void main(String[] args) {
        Pay pay = new Pay();
        pay.basePay();

        BigDecimal intCalc = new BigDecimal("0.15");

        Scanner userInput = new Scanner(System.in);

        System.out.println("What were your total sales?");
        BigDecimal salesPre = userInput.nextBigDecimal();
        System.out.println("You're Total Sales were "+salesPre);

        userInput.close();

        BigDecimal postIntCalc = salesPre.multiply(intCalc);
        BigDecimal salesCom = postIntCalc.add(salesPre);
        int finalCalc = salesCom.add(pay);

        System.out.println("Your total sales including commission is "+salesCom);
        System.out.println("Your total pay is"+finalCalc);
    }   
}

pay.java 文件如下:

import java.math.BigDecimal;

public class Pay {
    public void basePay() {
        int basePay = 50000;
        BigDecimal bd = new BigDecimal(String.valueOf(basePay));
    }
}

如果您希望支付 class 到 return 一个 basePay 值,您需要一个合适的方法,即实际 return 值的方法,即不是无效方法。

public class Pay {
    private int basePay = 50000;

    public BigDecimal getBasepay() {
        // no need to pass via strings, BigDecimal has a constructor that takes an int value
        BigDecimal bd = new BigDecimal(basePay);
        return bd;
    }
}

这将被称为

int finalCalc = salesCom.add(pay.getBasepay()).intValue();

因为你想将结果存储为整数,或者

BigDecimal finalCalc = salesCom.add(pay.getBasepay());

请注意,我将 basePay - 值 - 声明为 Pay class 的私有成员,并将该方法重命名为以 get 开头(在 Java 中称为 getter,按照惯例,它们的名称以 get 为前缀)。如果有一天您需要一种方法来修改该值,只需添加一个 setter

public void setBasepay(int bp) {
    basePay = bp;
}

也许您还希望能够将值直接设置为 BigDecimal,尽管它存储为 int?只需添加

public void setBasepay(BigDecimal bp) {
    basePay = bp.intValue();
}

两个同名但参数不同的方法称为重载,这是一种在程序中引入灵活性和通用性的常用机制。

我还建议你看看一个很好的教程,official one by Oracle 非常好(而且免费 :))

就像错误消息告诉你的那样,BigDecimaladd 方法只有一个参数需要一个 BigDecimal 实例:[javaDoc]

public BigDecimal add(BigDecimal augend)

Returns a BigDecimal whose value is (this + augend), and whose scale is max(this.scale(), augend.scale()).

Parameters:
augend - value to be added to this BigDecimal.

Returns:
this + augend

您已将 Pay 类型的变量传递给此方法,并且由于 Pay 不是 BigDecimal 的子类型,因此与它无关。方法 add 不知道如何添加 Pay 实例,编译器会抱怨该参数类型。

您可以执行以下修复以绕过该问题:

您的 basePay 方法创建了一个 BigDecimal,我猜这就是您想添加到 salesCom 的方法,所以稍微改变一下该方法:

public BigDecimal basePay() {
    int basePay = 50000;
    return new BigDecimal(String.valueOf(basePay));
}

此方法现在创建一个 BigDecimal 和 returns 到调用方法。现在将 add 方法调用更改为使用 basePay 方法:

int finalCalc = salesCom.add(pay.basePay());

现在只剩下一个问题了。正如您在上面发布的 JavaDoc 中看到的那样,add returns 一个新的 BigDecimal 实例,但是您将返回值分配给变量 finalCalc,它的类型int。所以我们需要把它改成BigDecimal:

BigDecimal finalCalc = salesCom.add(pay.basePay());

现在您的代码可以编译,它应该可以按预期工作。