简体   繁体   中英

Why doesn't my code show presentValue?

package presentvalue; import java.util.Scanner;

public class 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);
}

} The question says write a method presentValue that preforms this calculation. The method should accept the future value, annual interest rate, and number of years as arguments. It should return the present value, which is the amount that you need to deposit today. Demonstrate the method in a program that lets the user experiment with different values for the formula's terms.

Here is the formula P = F/(1+r)^2

You will have to use Math.pow :

public static double presentValue(double future, double intrest, double years)
{
    return future / Math.pow(1.0 + intrest, years);
}

Note that I added the amount of years. You stated two years, but I think you really want to have a parameter to specify the number of years, because, otherwise you wouldn't ask the user for an amount of years, right?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM