每月付款输入计算器

时间:2015-09-23 20:16:43

标签: java

我对java很新,而且我正在尝试编写一个程序,它会给我每月付款,利息总额和银行贷款支付的总金额,但我相信我的数学错误或不正确格式化,因为当我运行它时,我得到负数中的数字,我知道的付款是错误的。你能指出我犯错的地方吗?

示例:

7500 (amount borrowed) 
14.5 (loan rate)
3 (# of years)

预期输出为

258.16 (monthly payment)
1793.66 (interest paid)
9293.66 (total paid).

代码:

import java.io.*;
import java.util.*;
public class Prog58i
{
public static void main(String args[])
  {
    Scanner numberReader = new Scanner(System.in);
    System.out.print("The amount I wish to borrow is? ");
    int p = numberReader.nextInt();
    System.out.print("The loan rate I can get is? ");
    double r = numberReader.nextDouble();
    System.out.print("How mny years will it take me to pay off the loan? ");
    int m = (numberReader.nextInt())*12; 

    double MP = (1 +(r/1200)); 
    MP = Math.pow(MP, m);
    double payment = p *(r/1200) * (MP/(MP-1));
    payment = (int)(m * 100+0.5)/100.0;
    double total = (int)((m * payment)*100)/100.0;
    double intetotal = (int)((total - p)*100)/100.0;

    System.out.println("My monthly payments will be " + payment);
    System.out.println("Total Interest Paid is " + intetotal);
    System.out.println("Total amount paid is " + total);
   }
}

1 个答案:

答案 0 :(得分:2)

根据你的公式,这个陈述似乎是错误的

double MP = (1 + (r / 1200));

MP = Math.pow(MP, m);

电源仅在(r / 1200)而非(1 + (r / 1200))