⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 compoundinterest.java

📁 Java程序设计技巧与开发实例附书源代码。
💻 JAVA
字号:
import java.text.*;

public class CompoundInterest {
  final static int NUM_YEARS = 10;
  final static int NUM_PERIODS = 12;
  final static double RATE = 6.5;
  final static double START_BALANCE = 10000;

  public static double interestPerYear(double amount) {
    double compoundInterest = 0.0;
    for (int i = 0; i < NUM_PERIODS; i++) {
      double interest = amount * RATE / 100 / NUM_PERIODS;
      amount += interest;
      compoundInterest += interest;
    }
    return compoundInterest;
  }

  public static void adjustBalances(double amount[], double interest[]) {
    for (int year = 1; year < NUM_YEARS; year++) {
      interest[year] = interestPerYear(amount[year - 1]);
      amount[year] = amount[year - 1] + interest[year];
    }
  }

  public static double[] prefixAvg(double A[]) {
    double avg[] = new double[A.length];
    for (int year = 0; year < A.length; year++) {
      double sum = 0.0;
      for (int j = 0; j <= year; j++) {
        sum += A[j];
      }
      avg[year] = sum / (year + 1);
    }
    return avg;
  }

  public static void showAmounts(double amount[], double interest[],
                                 double avg[]) {
    DecimalFormat formatTool = new DecimalFormat("#,###.00");
    for (int i = 0; i < NUM_YEARS; i++) {
      System.out.print("Year:\t" + (i + 1));
      System.out.print("\tInt: " + formatTool.format(interest[i]));
      System.out.print("\tAmt: " + formatTool.format(amount[i]));
      System.out.println("\tAvg. Int: " + formatTool.format(avg[i]));
    }
  }

  public static void main(String args[]) {
    double amount[] = new double[NUM_YEARS];
    double interest[] = new double[NUM_YEARS];
    interest[0] = interestPerYear(START_BALANCE);
    amount[0] = START_BALANCE + interest[0];
    adjustBalances(amount, interest);
    showAmounts(amount, interest, prefixAvg(interest));
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -