2015-10-21 50 views
0
package methods; 
import java.util.Scanner; 

/** 
* 
* @author Billy 
*/ 
public class Methods { 

/** 
* @param args the command line arguments 
*/ 
    public static void main(String[] args) {  
    double Loanamount; // Loan amount from user 
    double Aintrate; // User's annual interest rate 
    double months; // Number of months on loan 
    double monp; // Monthly payment 
    double Mintrate; // Monthly interest rate 
    double n; // The number of payments 


    // declare an instance of Scanner to read the datastream from the keyboard. 
    Scanner kb = new Scanner(System.in); 

    // Get user's loan amount 
    System.out.print("Please enter your total loan amount: "); 
    Loanamount = kb.nextDouble(); 

    // Get user's interest rate 
    System.out.print("Please enter your annual interest rate as a decimal: "); 
    Aintrate = kb.nextDouble(); 

    // Get user's number of months 
    System.out.print("Please enter the number of months left on your loan: "); 
    months = kb.nextDouble(); 

    // Calculate montly interest rate 
    Mintrate = ((Aintrate/12)); 

     System.out.println("Your monthly interest rate is " + " " + Mintrate); 

    // Calculate number of payments 
     n = ((months * 12)); 

    // Calculate monthly payment 

我的下一個工作是找出每月付款使用這個公式。努布需要一些幫助實施一個公式

M = P [I(1 + I)^ N]/[(1 + I)^ N - 1]

M =每月按揭付款
P =借用(Loanamount)的量
I =每月利率(Mintrate)
N =支付次數

我嘗試以下,但我只是不能想出辦法來
​​

+0

使用'Math.pow',這就是你正在尋找的,而不是''''','''''''''''''','''''''''''''',''''''''''''','''''',''''''''''''''''''''''''''''''''',''''''''''''''''',''''''''''''''''''''''''''''還有,你的代碼應該包括這個形式, – SomeJavaGuy

+0

如果使用'double'類型仍然足以進行估計,則由於舍入誤差,它不應該用於財務計算。 –

回答

1

您需要使用Math.pow這裏有效。

嘗試在你的代碼

monp = Loanamount*(Mintrate*Math.pow((1+ Mintrate),n))/(Math.pow((1+ Mintrate),n-1) ; 
+0

非常感謝:) – archer

+0

@archer很高興幫助 –

1

^的方法是在Java中稱爲Math.pow(double a, double b);使用以下。

其中a是要提高的數字b次。

那麼你的公式看起來像monp = Loanamount Math.pow((Mintrate(1+ Mintrate), n))/(Math.pow((1+ Mintrate), n-1));

Reference

0

你需要調用Math.pow(雙,雙)

,或者你可以導入所需的類作爲..
import static java.lang.Math.pow;
然後使用pow()函數