gpt4 book ai didi

java - 帐户类 Java OOP

转载 作者:行者123 更新时间:2023-12-01 08:54:32 25 4
gpt4 key购买 nike

我已经创建了一个帐户类,但在第一次计算之后,它继续并将第二行加倍。我是否错过了代码中的任何内容?

public class Account
{
private double balance; //STATE
private double interestRate; //STATE
private double rate;//STATE

public Account()
{
balance = 0;
interestRate = 0;
}

public Account(double amount, double interestRate)
{
balance = amount;
rate = interestRate;

}

public void deposit(double amount)
{
balance=balance+amount;
}

public void withdraw(double amount)
{
balance = balance - amount;
}

public void setInterest(double rate)
{
balance = balance + balance * rate;
//this.setInterst = setInterest;
//setInterest = InterestRate / 12;
}

public double computeInterest(int n)
{
balance=Math.pow(balance*(1+rate),n/12);
return balance;
}

public double getsetInterest()
{
return rate;
}

public double getBalance()
{
return balance;
}

public void close()
{
balance =0;
}

}

public class TestAccountInterest
{
public static void main (String[] args)
{
Account acc1 = new Account(500, 0.1);//0.10);
Account acc2 = new Account(400, 0.2); //0.20);

/*************************************
ACC1 ACCOUNT BELOW
*************************************/
acc1.deposit(500);
acc1.withdraw(300);
acc1.computeInterest(12);
acc1.computeInterest(24);
System.out.println(acc1.computeInterest(12));

/**************************************
ACC2 ACCOUNT BELOW
**************************************/
acc2.withdraw(200);
acc2.deposit(800);
acc2.computeInterest(24);
System.out.println(acc2.computeInterest(24));

}

}

不知道是我漏掉了什么还是代码写错了。

最佳答案

    acc1.computeInterest(12);
acc1.computeInterest(24);

在我看来,您想要的是调用这些函数仅返回计算出的利息,但它不应该更改您的余额变量。

仅返回计算值,而不将其保存在@balance变量中。

这是我对你问题的解释,你有点含糊。

关于java - 帐户类 Java OOP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42137561/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com