Java 我是否正确使用了参数?
所以我是一个初学者,你可能知道,我目前正在尝试理解参数,想知道我是否做得对。这个程序可以运行,但我不想把它格式化成错误的格式,或者可能是因为参数(?)没有改变,所以我无缘无故地使用了它们。我们目前正在研究参数,所以我想我应该试一试,并尝试将其纳入本作业Java 我是否正确使用了参数?,java,parameters,Java,Parameters,所以我是一个初学者,你可能知道,我目前正在尝试理解参数,想知道我是否做得对。这个程序可以运行,但我不想把它格式化成错误的格式,或者可能是因为参数(?)没有改变,所以我无缘无故地使用了它们。我们目前正在研究参数,所以我想我应该试一试,并尝试将其纳入本作业 import java.util.Scanner; public class Investments { public static void main(String []args) {
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;
}
}
看起来不错!如果你想让你的投资功能更加强大,你可以把所有的逻辑都放到这里,只需输入投资、利率和投资周期
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)您是否尝试过编译和运行它,这是检查代码是否有效的最佳方法:-)?我觉得还可以。谢谢你的反馈。我确实运行了它,它工作了!我只是想知道我是否做对了