仅获得利息金额而不是我本应支付一个月的金额,您能告诉我哪里出了问题吗谢谢。

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);
}


}

最佳答案

您的错误在这里:

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 =长度/ 12来获取每月。您应该改为:

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

10-06 16:03
查看更多