【问题标题】:Why doesn't my code show presentValue?为什么我的代码不显示 presentValue?
【发布时间】:2013-12-03 11:57:42
【问题描述】:

包裹现值; 导入 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

【问题讨论】:

  • 你的问题是? (你已经展示了你的代码和原始问题,但你没有说什么是失败的。)作为风格问题,有点遗憾的是你有两个方法叫做 presentValue 做非常不同的事情...
  • 抱歉,它不计算 presentValue。
  • 那么它做什么呢?您的输入、预期输出和实际输出是多少?您采取了哪些步骤来诊断问题?目前,您的问题给人的印象是“这已损坏,请修复它”,而不是真正尝试自己修复并学习。

标签: java


【解决方案1】:

你必须使用Math.pow:

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

请注意,我添加了年数。您说的是两年,但我认为您确实希望有一个参数来指定年数,因为否则您不会向用户询问年数,对吗?

【讨论】:

  • 不是years,根据OP的公式P = F/(1+r)^2,它将是2
  • 我知道,但我怀疑 OP 实际上想要年数。否则他不会提示用户输入年数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-08
  • 2010-11-04
  • 1970-01-01
  • 1970-01-01
  • 2021-01-03
  • 2023-03-31
相关资源
最近更新 更多