我正在尝试编写一个付款计算器程序,但收到的结果是“NaN”。该计划要求输入贷款金额和贷款期限(以月为单位)。然后,程序应计算出每个年利率 (3% -10%) 的每月付款。我不确定我的计算是否有问题。
double L, payment;
double APR = 0;
int n;
Đầu vào máy quét = Máy quét mới (System.in);
System.out.println("Loan calculator");
System.out.print("Enter the loan amount: ");
L = input.nextDouble();
System.out.print("Enter the number of payments: ");
n = input.nextInt();
double t = APR/1200.0;
for (APR = 3; APR <= 10; APR += .25){
payment = L * (t * (Math.pow((1.0 + t), n)) / (Math.pow((1.0 + t), n) - 1.0));
System.out.println(APR + "\t" + payment);
在循环内定义或至少分配 t。否则只会在循环之前使用 APR 计算一次,此时 APR 为 0。
for (APR = 3; APR <= 10; APR += .25){
double t = APR/1200.0;
payment = L * (t * (Math.pow((1.0 + t), n)) / (Math.pow((1.0 + t), n) - 1.0));
System.out.println(APR + "\t" + payment);
Tôi là một lập trình viên xuất sắc, rất giỏi!