2013-10-21 41 views
3

我之前沒有使用java,我很困惑爲什麼我寫的簡單的現值計算器不工作。現值公式由於某種原因返回一個超小號碼?看看你是否能發現我的錯誤:簡單的Java現值計算器不能正常工作

// Import all utilities 
import java.text.DecimalFormat; 
import java.util.*; 

// Base class 
public class Project2 
{ 

    // Main function 
    public static void main(String[] args) 
    { 
     // Define variables 
     double p = 0.0; 
     double f = 0.0; 
     double r = 0.0; 
     double n = 0.0; 
     String another = "Y"; 

     // Create a currency format 
     DecimalFormat dollar = new DecimalFormat("#,###.00"); 

     // Create a new instance of the scanner class 
     Scanner keyboard = new Scanner(System.in); 

     // Loop while another equals "Y" 
     while(another.equals("Y")) 
     { 
     // Get future value 
     System.out.println("Future value: "); 
     f = Double.parseDouble(keyboard.nextLine()); 

     // Get annual interest rate 
     System.out.println("Annual interest rate: "); 
     r = Double.parseDouble(keyboard.nextLine()); 

     // Get Number of years 
     System.out.println("Number of years: "); 
     n = Double.parseDouble(keyboard.nextLine()); 

     // Run method to find present value and display result 
     p = presentValue(f, r, n); 
     System.out.println("Present value: $" + p); 

     // Ask if user wants to enter another 
     System.out.println("Enter another?(Y/N) "); 
     another = keyboard.nextLine().toUpperCase(); 
     } 

    } 

    public static double presentValue(double f, double r, double n) 
    { 
     // Do math and return result 
     double p = f/Math.pow((1 + r), n); 
     return p; 
    } 
} 
+0

「看你是否能發現我的錯誤」 - 這將是*好得多*如果你鑑定和分離您認爲代碼的一部分,會造成您的錯誤。我們無法隨意抽查您的代碼... – Makoto

+0

將它縮小就是現在必須執行的操作,因爲您的當前值計算除以大於1的數字。 – Radiodef

+1

問題是什麼?你說「它返回一個超級小的價值」。期望值是多少?什麼是實際價值?你有錯誤嗎?那是什麼錯誤?如果你回答這些問題,你很可能會解除阻滯。 – Shobit

回答

-2

Math.pow期望第一個參數是基數,第二個參數是指數。 (見The Math javadoc

在你的程序中,第一個參數是權力,第二個參數是基礎。

將其改爲double p = f/Math.pow(n,(1 + r));我希望你的程序按預期運行

+0

呃......不,不是。 'pow'功能正確使用。檢查javadoc。請在發佈之前閱讀它。 – user2339071

-2

你的程序工作正常。我只是測試它。

Future value: 
100000 
Annual interest rate: 
0.4 
Number of years: 
20 
Present value: $119.519642774552 
Enter another?(Y/N) 
Y 
Future value: 
100000 
Annual interest rate: 
40 
Number of years: 
20 
Present value: $5.550381891760752E-28 
Enter another?(Y/N) 

您可能輸入的利率錯誤。

如果要輸入的整數值,可以修改公式:

double p = f/(Math.pow((1 + (r/100.0)), n)); 

這將導致:

Future value: 
100000 
Annual interest rate: 
40 
Number of years: 
20 
Present value: $119.519642774552 
Enter another?(Y/N) 
+0

錯了。再次檢查javadoc。第一個參數是基數。第二個是權力。 – user2339071

+0

哦,所以你想把興趣放在多少年的力量上? – dtgee

+0

如果它複合,是的。如果利息是0.01,那麼在第一年之後,價值將是1.01,在第二年之後它將是1.01。 – Radiodef

1

假設你輸入R作爲% per annum即對於例如R = 4.3%,你將要修改的功能:

double p = f/(Math.pow((1 + (r/100.0)), n)); 
return p; 

如果這不是你想什麼,你可能想進入的R=4.3% p.a值作爲

4.3/100 = 0.043,而不是4.3

0

而不是未來價值請爲委託人提供意見。

您的PresentValue計算功能就是這樣。試試這個功能,希望你能得到你的完美的結果

public double presentValue(double principal, double yearlyRate, double termYears) 
{ 
    // Do math and return result 
    double pValue = principal * (((1- Math.pow(1 + yearlyRate, -termYears))/ yearlyRate)); 
    return pValue; 
}