我是否正确使用参数?

时间:2016-02-05 09:38:33

标签: java parameters

所以我是一个初学者,你可以告诉我,我正在尝试理解参数,并想知道我是否正确地做到了这一点。该程序有效,但我不想让它格式化错误或者我没有理由使用它们,因为参数(?)不会改变。我们目前正在研究参数,所以我想我会试一试并尝试将其包含在这项任务中。

    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;
    }
}

1 个答案:

答案 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);

(等)