包现值; import java.util.Scanner;
公共类PresentValue {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
double P; // present value
double F; // future value
double r; // annual interest rate
double n; // number of years
P = presentValue();
F = futureValue();
r = annualInterest();
n = years();
System.exit(0);
}
public static double presentValue()
{
int input;
Scanner keyboard = new Scanner(System.in);
System.out.println("What is the presnt value of you savings account?");
return keyboard.nextDouble();
}
public static double futureValue()
{
int input;
Scanner keyboard = new Scanner(System.in);
System.out.println("How much do you want in your account in the future?");
return keyboard.nextDouble();
}
public static double annualInterest()
{
int input;
Scanner keyboard = new Scanner(System.in);
System.out.println("What is youe bank's interest rate?");
return keyboard.nextDouble();
}
public static double years()
{
int input;
Scanner keyboard = new Scanner(System.in);
System.out.println("How many years will the money in the bank?");
return keyboard.nextDouble();
}
public static double presentValue(double F, double r)
{
return F/(1+(r * r));
}
public static void show(double presentValue)
{
System.out.println(presentValue);
}
} 问题是写一个方法presentValue,它预先形成这个计算。该方法应接受未来价值,年利率和年数作为参数。它应该返回当前值,即您今天需要存入的金额。在程序中演示该方法,让用户体验公式术语的不同值。
这是公式P = F /(1 + r)^ 2
答案 0 :(得分:0)
您必须使用Math.pow
:
public static double presentValue(double future, double intrest, double years)
{
return future / Math.pow(1.0 + intrest, years);
}
请注意,我添加了年数。你陈述了两年,但我认为你真的想要一个参数来指定年数,因为,否则你不会要求用户多年,对吗?