用数学函数读取计算错误

时间:2012-11-19 14:45:24

标签: java

我只得到的利息金额不是我想支付一个月的金额,请你告诉我哪里出错了。谢谢。

import java.util.Scanner;

/**
 *
 * @author 
 */
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//variabled decleared
double rate;
double payment;
//input
System.out.print("Enter Loan Amount:");
double principal = input.nextDouble();
System.out.print("Enter Annual Interest:");
double interest = input.nextDouble();
System.out.print("Total payment type:");
String period = input.next();
System.out.print("Enter Loan Length :");
int length = input.nextInt();

//proces
rate = interest / 100;

if (period.equals("monthly")) {
    double n = length * 12;
    payment = principal * (rate * Math.pow((1 + rate), n) / Math.pow((1 + rate), n));
    System.out.printf("Your Monthly Sum is %.2f",payment);
}

}

2 个答案:

答案 0 :(得分:2)

您的错误在这里:

principal * rate * Math.pow((1 + rate), n) / Math.pow((1 + rate), n)

这与只有主*率相同。你是说x = b * a / a。 替换为:

 payment = principal * Math.pow((1 + rate), n);

n是年数,你不能做n = length / 12来获得每月。你应该这样做:

payment = (principal * Math.pow((1 + rate), n)) / 12;

答案 1 :(得分:1)

应该是

payment = principal * Math.pow((1 + rate), n);

作为

A=P((1+rate/100)^n)