2016-02-05 154 views
0

所以我是一個初學者,你可以告訴我,我目前正在嘗試理解參數,並想知道我是否正確地做了這個。該程序的工作原理,但我不想讓它格式錯誤,或者我沒有理由使用它們,因爲參數(?)不會改變。我們目前正在研究參數,所以我想我會試一試,並嘗試將其納入本作業。我使用了正確的參數嗎?

import java.util.Scanner; 

    public class Investments 
    { 
     public static void main(String []args) 
    { 

     double investment,interestRate, futureValue; 

     Scanner keyboard = new Scanner(System.in); 

     System.out.println("What is the investment amount?"); 
     investment = keyboard.nextDouble(); 

     System.out.println("What is the interest rate?"); 
     interestRate=keyboard.nextDouble(); 

     futureValue = investment * (Math.pow(1+interestRate, 5)); 
     futureValue = investments(futureValue); 
     System.out.println("Your investment afer 5 years:" + futureValue); 

     futureValue = investment * (Math.pow(1+interestRate, 10)); 
     futureValue = investments(futureValue); 
     System.out.println("Your investment after 10 years:" + futureValue); 

     futureValue = investment * (Math.pow(1+interestRate, 20)); 
     futureValue = investments(futureValue); 
     System.out.println("Your investment after 20 years:" + futureValue); 

    } 

    public static double investments (double futureValue) 
    { 
     double result; 

     result = Math.round(futureValue * 100); 
     result = result/100; 
     return result; 
    } 
} 
+0

你試過編譯並運行它,那是要檢查的最佳途徑,如果你的代碼工作:-) ?對我來說似乎沒問題。 – ctst

+0

感謝讚賞反饋。我確實運行它,它的工作原理!我只需要知道我是否做得正確 –

回答

0

這看起來不錯!如果你想讓你的投資功能更強大,你可以把所有的邏輯轉移到那裏,只需要通過投資,利率和投資期。

public static double investments (double investment, double interestRate, int numYears) 

然後你的主要功能可以只是:

futureValue = investments(interestRate, 5); 
System.out.println("Your investment after 5 years:" + futureValue); 

futureValue = investments(interestRate, 10); 
System.out.println("Your investment after 10 years:" + futureValue); 

(ETC)