我在帐户类别中有以下方法

public void Transfer(int accountNo, double amount)
{
    this.accountNo = accountNo;
    double savingBal= this.getSavingBal();
    if (this.savingBal > amount)
    {this.savingBal=-amount;
    this.checkingBal=+amount;
    }
    else
    {
        System.out.println("Insufficent fonds into savings account");
    }

}


并将此代码放入Main Class:

//

 Account b = new Account();


//

  System.out.println("Please mention the amout you whish to transfer and the account");
            amount = s.nextDouble();
            accountNo = s.nextInt();
            b.Transfer(accountNo, amount);


但是打印到控制台的结果始终是:
“资金不足”

请帮忙,

真诚的

完整帐户类别

package atmsimulation;
import java.util.*;

public class Account {
private int accountNo;
private double checkingBal;
private double savingBal;

//Scanner sc = new Scanner(System.in);

Account ()
        {
        this.checkingBal=0;
        this.savingBal=0;
        this.accountNo=0;
        }

  public Account(int accountNo, double checkingBal, double savingBal) {
        this.accountNo = accountNo;
        this.checkingBal = checkingBal;
        this.savingBal = savingBal;
    }
//makes a @Payment
public void Payment(double amount)
{
    this.checkingBal=-amount;
}
//makes a @Deposit
public void Deposit(double amount)
{
    this.checkingBal=+amount;
}
//makeks a transfer from @savings into @checking account
public void Transfer(int accountNo, double amount)
{
    this.accountNo = accountNo;
    double savingBal= this.getSavingBal();
    if (this.savingBal > amount)
    {this.savingBal=-amount;
    this.checkingBal=+amount;
    }
    else
    {
        System.out.println("Insufficent fonds into savings account");
    }

}

    public void setCheckingBal(double checkingBal) {
        this.checkingBal = checkingBal;
    }

    public void setSavingBal(double savingBal) {
        this.savingBal = savingBal;
    }

    public int getAccountNo() {
        return accountNo;
    }

    public void setAccountNo(int accountNo) {
        this.accountNo = accountNo;
    }

      public double getSavingBal() {
        return this.savingBal;
    }

    public double getCheckingBal() {
        return this.checkingBal;
    }



}

最佳答案

this.savingBal=-amount;


是错字,应该是

this.savingBal-=amount;


下一行也有类似的错字。这些可能是造成您的问题的原因。如果不是这样,并且存在另一个问题,那么如果您解决了另一个问题,它们将引起同样的问题。

关于java - 实现从储蓄账户到支票账户的银行转账的Java方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20315913/

10-10 00:53