Java使用方法并调用它们

时间:2014-09-06 00:40:04

标签: java methods

package savingsaccount;

public class SavingsAccount
{
    static double AnnualInterestRate;
    static double savingsBalance;

    public static double setInterestRate(double annualInterestRate) {
        annualInterestRate=.04;
        return annualInterestRate;
    }

    public static double calcnewbalance(double newsaverbal) {
        AnnualInterestRate = setInterestRate(AnnualInterestRate);
        newsaverbal = (newsaverbal * AnnualInterestRate/12) + newsaverbal;
        return newsaverbal;
    }

    public static void main(String[] args) {
        System.out.println("Savings    Account    Balances");
        System.out.println("Month    Saver1    Saver2");
        double saver1=2000;
        double saver2=3000;
        for(int n=1; n<=13; n=n+1) {
            if(n<10) {
                savingsBalance = calcnewbalance(saver1);
                saver1 = savingsBalance;
                System.out.printf( " "+ n + "        %.2f   " , saver1);
                savingsBalance = calcnewbalance(saver2);
                saver2 = savingsBalance;
                System.out.printf("%.2f%n" , savingsBalance);

            } else if(n<13 && n>9) {
                savingsBalance = calcnewbalance(saver1);
                saver1 = savingsBalance;
                System.out.printf(n + "        %.2f   " , saver1);
                savingsBalance = calcnewbalance(saver2);
                saver2 = savingsBalance;
                System.out.printf("%.2f%n" , saver2);
            } else {
                savingsBalance = calcnewbalance(saver1);
                saver1 = savingsBalance;

                System.out.printf(n + "        %.2f   " , saver1);
                savingsBalance = calcnewbalance(saver2);

                saver2 = savingsBalance;
                System.out.printf("%.2f%n" , saver2);
            }
        }
    }
}

所以我试图将利率设置为代码的“其他”部分到不同的值。我想仅在方程计算新余额中使用setInterestRate。

setInterestRate有没有办法知道它何时被调用第13次并实例化利率的新值?

1 个答案:

答案 0 :(得分:0)

原语不是Object,并且未实例化。而且,使这些静态完全没有意义 -

static double savingsBalance;
static double AnnualInterestRate;
public static double setInterestRate(double annualInterestRate) {
    annualInterestRate=.04;
    return annualInterestRate;
}

应该是一个存取器和一个mutator(或一个getter和setter),

private double annualInterestRate;
public void setInterestRate(double annualInterestRate) {
  this.annualInterestRate = annualInterestRate;
}
public double getInterestRate() {
  return this.annualInterestRate;
}
private double balance;
public double getBalance() {
  return balance;
}

然后

public double calcnewbalance(double balance) {
    AnnualInterestRate = setInterestRate(AnnualInterestRate); // 0.4 is always 0.4
    newsaverbal += (newsaverbal * getInterestRate()/12);
    return newsaverbal;
}

可以

public void applyInterest() {
    balance += (balance * getInterestRate()/12);
}

接下来,一个no-arg SavingsAccount构造函数 -

public SavingsAccount(double balance) {
  this.annualInterestRate = 0.04;
  this.balance = balance;
}

最后在main()

SavingsAccount sa = new SavingsAccount();
for (int = 0; i < 13; i++) {
  System.out.println(sa.getBalance());
  sa.applyInterest();
}

修改

要改变第13次通话的利率,你可以这样做 -

private int interestCount = 0;
public void applyInterest() {
    if (interestCount++ > 12) {
      interestCount = 0;
      setInterestRate(0.05);
    }
    balance += (balance * getInterestRate()/12);
}