首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Account类Java OOP

Account类Java OOP
EN

Stack Overflow用户
提问于 2017-02-09 21:06:17
回答 2查看 641关注 0票数 0

我已经创建了一个account类,但在第一次计算之后,它继续并将第二行加倍。我是不是在代码中遗漏了什么。

代码语言:javascript
复制
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));

    }

}

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

EN

回答 2

Stack Overflow用户

发布于 2017-02-09 21:14:13

代码语言:javascript
复制
    acc1.computeInterest(12);
    acc1.computeInterest(24);

在我看来,你想要的是调用这些函数只返回计算出的利息,但它不应该改变你的平衡变量。

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

这是我对你的问题的理解,你有点含糊其辞。

票数 1
EN

Stack Overflow用户

发布于 2017-02-09 21:22:03

您已经为对象acc1使用了方法computeinterest(int,n)两次。当你第一次使用acc1.computeInterest(12)时,你得到了一个值,但是当你使用acc1.computeInterest(24)之后,你得到的答案是不正确的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42137561

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档