我必须在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));
}
}
它没有编译,我真的很沮丧,因为我花了很长时间,我无法弄明白。
感谢任何帮助
答案 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));