摊销的例子

时间:2018-03-26 14:49:56

标签: java decimalformat

这是我的代码:

  public class exampleaAmortizationProgram {
 public static void main(String[] args) {
     Scanner input = new Scanner(System.in);

     System.out.printf("Loam amount");
     double loamAmount = input.nextDouble();

     System.out.print("Number of Years:");
     int numberOfYears = input.nextInt();

     System.out.print("Annual Interest Rate(8.25): ");
     double annualInterestRate = input.nextDouble();

     double monthlyInterestRate = annualInterestRate / 1200;

     //compute mortgage 
     double monthlyPayment = monthlyPayment(loamAmount, numberOfYears, monthlyInterestRate);

     double balance = loamAmount;
     double interest;
     double principal;

     System.out.println("Monthly Payment: " + monthlyPayment * 100/ 100.0);
     System.out.println("Total Payment: " + monthlyPayment * 12 + numberOfYears * 100/ 100.0 + "\n");

    DecimalFormat df = new DecimalFormat("0.00");

    for (int i = 1; i <= numberOfYears * 12; i++) {
        interest = monthlyInterestRate * balance * 100 / 100.0;
        principal = monthlyPayment - interest * 100 / 100.0;
        balance = balance - principal * 100 / 100.0;

        System.out.println(i + "\t\t" + df.format(interest) + "\t\t" + df.format(principal) + "\t\t" + df.format(balance));
    }       
    }
private static double monthlyPayment(double loamAmount, int numberOfYears, double monthlyInterestRate) {
    double monthlyPayment = loamAmount + monthlyInterestRate / (1 - (Math.pow(1 / (1 + monthlyInterestRate), numberOfYears * 12)));
    return monthlyPayment;
}
}

我已经查看了使用DecimalFormat的不同方法,但它仍然说我在该代码源中有一个错误:

exampleaAmortizationProgram.java:44: error: 
cannot find symbol DecimalFormat df = new DecimalFormat("0.00"); 

请帮助。

1 个答案:

答案 0 :(得分:0)

  

System.out.println(i +&#34; \ t \ t&#34; + df.format(兴趣)+&#34; \ t \ t&#34; -   df.format(principal)+&#34; \ t \ t&#34; + df.format(balance));

你在那里“ - ”。