汽车支付应用涉及具有负指数的等式

时间:2015-01-08 22:28:34

标签: java equation-solving

我必须在java中创建一个汽车支付应用程序,提示用户支付本金(P),利率(r)和每月支付的数量(m)每月的汽车支付必须使用以下公式计算:

P(R / 12)/(1-(1 + R / 12)^ - 米)

这就是我到目前为止......

 import java.util.Scanner;
 import java.lang.Math; //importing Scanner, Math, and NumberFormat classes
 import java.text.NumberFormat;

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

        double principal, rate, numberOfMonths, payment;    //declaring varibles

        System.out.print("Principal: ");
        principal=input.nextDouble();
        System.out.print("Interest Rate: ");    //requesting and storing user input
        rate=input.nextDouble();
        System.out.print("Number of monthly payments: ");
        numberOfMonths=input.nextDouble();
        input.close(); //closing Scanner

        payment=principal*(rate/12)/(1-(1+rate/12*)Math.pow(payment, -numberOfMonths)); //calculating monthly payment. Error in this line

        NumberFormat money =NumberFormat.getCurrencyInstance(); //Formatting output
        System.out.println("The monthly payment is:" (money.format(payment));



    }
 }

它没有编译,我真的很沮丧,因为我花了很长时间,我无法弄明白。

感谢任何帮助

2 个答案:

答案 0 :(得分:1)

我认为如果你把你的公式分成小块就好了

   double rate1 = rate / 12 / 100;    // monthly interest rate
    double numberOfMonths = 12 * Y;         // number of months

    double payment  = (Principal * rate1) / (1 - Math.pow(1+rate1, -numberOfMonths));

我希望有帮助

答案 1 :(得分:0)

<强>式

你拥有的和错误:

payment=principal*(rate/12)/(1-(1+rate/12*)Math.pow(payment, -numberOfMonths));
  • 二元运算符*在表达式(1+rate/12*)
  • 中没有第二个参数
  • 未初始化的payment变量,用作Math.pow()
  • 的第一个参数
  • 上述声明
  • 未实现所需的公式P(r/12)/(1-(1+r/12)^-m)

P(r/12)/(1-(1+r/12)^-m)应该是什么:

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

<强>输出

你拥有的和错误:

System.out.println("The monthly payment is:" (money.format(payment));
  • 文字字符串和格式化付款之间缺少字符串连接运算符+
  • 目前尚不清楚利率是被解释为百分比还是直小数

为清晰起见应该是什么:

System.out.println("Rate: " + NumberFormat.getPercentInstance().format(rate));
System.out.println("Payment: " + NumberFormat.getCurrencyInstance().format(payment));