2012-07-06 89 views
1

當我爲每月利率和1年的投資金額4.25輸入1000時,爲什麼我會得到結果4.384414858452464E11而不是預期的1043.34?Math.pow難度

import java.util.Scanner; 

public class FinancialApplicationFutureInvestment_13 { 
    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 
     System.out.println("Enter investment amount: "); 
     int investmentAmount = input.nextInt(); 
     System.out.println("Enter monthly interest rate: "); 
     double monthlyInterestRate = input.nextDouble(); 
     System.out.println("Enter number of years"); 
     int numOfYears = input.nextInt(); 

     double futureInvestmentValue = investmentAmount * 
      (Math.pow(1 + monthlyInterestRate, numOfYears * 12)); 
     System.out.println("Accumulated value is: " + futureInvestmentValue); 

     double test = Math.pow(1 + monthlyInterestRate, numOfYears * 12); 
     System.out.println(test); 
    } 
} 
+0

你在哪兒'1043.34'?如果我按照下面的建議將利率除以100,那麼我得到了'1647.83'。 – Keppil 2012-07-06 09:10:27

+0

每月4.25%〜64.7%每年。這應該會產生很大的結果。 – 2012-07-06 09:31:46

回答

5

月息大概將需要輸入爲0.0425

+0

使用時0.0425我得到1647.8313602236476 – 2012-07-06 09:12:22

+0

這真的是解決辦法嗎?要改變程序規格以減輕用戶負擔? – Potatoswatter 2012-07-06 09:12:31

+0

答案應該是1043.34 – 2012-07-06 09:13:29

2

1 + monthlyInterestRate

monthlyInterestRate原始因素,抑或是在個百分點表達?

嘗試除以一百。

+0

它是百分比 – 2012-07-06 09:11:14

+0

@ Doesn'tMatter它是修辭問題; v) – Potatoswatter 2012-07-06 09:11:45

2

公式是

A = P(1+r/100)^n 

所以它應該是

investmentAmount * (Math.pow(1 + (monthlyInterestRate/100), numOfYears * 12)); 
+0

我仍然得到一個錯誤的答案,我得到1647.8313602236476而不是1043.34 – 2012-07-06 09:15:41

+0

利率是每月或每年? – 2012-07-06 09:17:36

+0

我認爲這是昂貴的,但結果是每年 – 2012-07-06 09:21:08

2

好,你計算1 + 4.25(5.25)作爲每月利率,而不是1+(4.25/100)。

1

你應該使用的System.out.println鑄造(雙到INT) - (INT)(futureInvestmentValue * 100)/ 100.0