我已经创建了一个account类,但在第一次计算之后,它继续并将第二行加倍。我是不是在代码中遗漏了什么。
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));
}
}我不知道是我遗漏了什么,还是我写错了代码。
发布于 2017-02-09 21:14:13
acc1.computeInterest(12);
acc1.computeInterest(24);在我看来,你想要的是调用这些函数只返回计算出的利息,但它不应该改变你的平衡变量。
只需返回计算值,而不将其保存在@balance变量中。
这是我对你的问题的理解,你有点含糊其辞。
发布于 2017-02-09 21:22:03
您已经为对象acc1使用了方法computeinterest(int,n)两次。当你第一次使用acc1.computeInterest(12)时,你得到了一个值,但是当你使用acc1.computeInterest(24)之后,你得到的答案是不正确的。
https://stackoverflow.com/questions/42137561
复制相似问题