Java 无法取消对Float的引用 类库{ 私人浮动转让; 私人浮动转让给; 公共银行(浮动转出,浮动转出){ this.transferFrom=transferFrom; this.transferTo=transferTo; 银行账户[]a=新银行账户[4]; for(int i=0;i金额){ 转让自widraw(金额); 转让至。存款(金额); } 抛出新的RuntimeException(“传输无法完成”); } }

Java 无法取消对Float的引用 类库{ 私人浮动转让; 私人浮动转让给; 公共银行(浮动转出,浮动转出){ this.transferFrom=transferFrom; this.transferTo=transferTo; 银行账户[]a=新银行账户[4]; for(int i=0;i金额){ 转让自widraw(金额); 转让至。存款(金额); } 抛出新的RuntimeException(“传输无法完成”); } },java,Java,我不知道为什么会出现这个错误。我对java相当陌生,无论我在哪里检查答案都比我所知道的更高级。你能帮我吗?谢谢。transferTo和transferFrom是float变量float是一个函数,不能对基元类型调用方法 您在哪里声明了getBalance()、widraw()和deposit()方法?您应该在该类型对象的实例上调用它们 这只是一个猜测,但它们可能在银行账户中?然后,您需要在BankAccount对象实例上调用这些方法,即: class Bank{ private

我不知道为什么会出现这个错误。我对java相当陌生,无论我在哪里检查答案都比我所知道的更高级。你能帮我吗?谢谢。

transferTo
transferFrom
float
变量
float
是一个函数,不能对基元类型调用方法

您在哪里声明了
getBalance()
widraw()
deposit()
方法?您应该在该类型对象的实例上调用它们

这只是一个猜测,但它们可能在银行账户中?然后,您需要在
BankAccount
对象实例上调用这些方法,即:

    class Bank{
    private float transferFrom;
    private float transferTo;

    public Bank(float transferFrom, float transferTo){
        this.transferFrom = transferFrom;
        this.transferTo = transferTo;
        BankAccount[] a = new BankAccount[4];
        for(int i = 0; i < a.length; i++){
            a[i] = new BankAccount(i);
        }
    }

    public void transfer(float amount){
        if(transferFrom.getBalance() > amount){
            transferFrom.widraw(amount);
            transferTo.deposit(amount);
        }
            throw new RuntimeException("Transfer could not be done.");
    }

}
这只是一个例子

@编辑:还要小心,在
transfer()
方法中,如果不将运行时异常放入
else
块中,则始终会得到
RuntimeException
。在Java中,如果遇到并完成
if
块,方法不会自动返回。另外,请不要只抛出运行时异常,创建您自己的、更有意义的异常。

这是如何编译的?(编译了吗?)


float
是一个原语,不包含这些方法。

我认为您希望将
transferFrom
transferTo
作为
transfer
类型的
transfer
方法的参数。您不能对基元
float
调用方法。您收到的错误是什么?transferFrom.getBalance()应该做什么?
private BankAccount transferFrom = new BankAccount(10);
private BankAccount transferTo = new BankAccount(10);

public void transfer(float amount){
    if(transferFrom.getBalance() > amount){
        transferFrom.widraw(amount);
        transferTo.deposit(amount);
    } else {
        throw new RuntimeException("Transfer could not be done.");
    }
}
transferFrom.widraw(amount);
transferTo.deposit(amount);