简体   繁体   中英

Car Payment App involving an equation with negative exponents

I have to create an Car Payment app in java that prompts user for principal owing (P), interest rate (r) and the number of monthly payments (m) The monthly car payment MUST be calculated using this formula:

P(r/12)/(1-(1+r/12)^-m)

This is what I have so far...

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



    }
 }

It does not compile, and I'm really frustrated because I have spent a long time and I cannot figure this out.

Any help is appreciated

I think it would be nice if you break your formula into little pieces

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

I hope that helps

Formula

What you have, and the errors:

payment=principal*(rate/12)/(1-(1+rate/12*)Math.pow(payment, -numberOfMonths));
  • binary operator * has no second argument in the expression (1+rate/12*)
  • uninitialized payment variable used as first argument to Math.pow()
  • required formula P(r/12)/(1-(1+r/12)^-m) is not realized by the above statement

What it should be for P(r/12)/(1-(1+r/12)^-m) :

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

Output

What you have, and the errors:

System.out.println("The monthly payment is:" (money.format(payment));
  • String concatenation operator + is missing between literal string and formatted payment
  • It's not clear whether interest rate is interpreted as a percentage or straight decimal

What it should be for clarity:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM