如何正确获得NaN作为结果?

时间:2019-10-25 20:12:36

标签: java nan

我正在尝试编写付款计算器程序,但结果为“ NaN”。该程序要求输入贷款金额和贷款期限(以月为单位)。然后,该程序应计算出每个APR的每月付款额(3%-10%)。我不确定我的计算是否做错了。

   double L, payment;   
   double APR = 0;   
   int n;   
   Scanner input = new Scanner(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);

1 个答案:

答案 0 :(得分:0)

在循环内定义或至少分配t。否则,它将仅在使用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);