如何在另一种方法的方程式中使用一种方法中的变量?

时间:2014-02-25 22:05:54

标签: java variables methods parameters console

所以,我正在为学校做这个项目,我觉得这很简单,但我真的很蠢。我需要有两种方法;一个只进行我想要完成的计算,另一个用户输入数字并显示计算方法的答案。来自班级网站的样本让人们很容易理解如何使用一个用户输入的变量,而不是像我需要的那样3;我还需要能够将它们相乘并在等式中使用它们。这是我到目前为止所做的:

import java.util.*;
import java.text.*;

public class Lab3StarterCodeExperimental {
  public static final Scanner CONSOLE = new Scanner(System.in);

  public static double compoundInterest(double accountValue) {
    double firstCalc = accountValue * 150;
    return firstCalc;
  }


  public static void main(String [] args) {
    //print title of lab
    System.out.println("Lab 3");

    //get user input (present value of account, interest rate, # years, payment per year)
    System.out.print("Enter present value of the account: ");
    double presentValue = CONSOLE.nextDouble();
    System.out.print("Enter the interest rate of the account: ");
    double interestRate = CONSOLE.nextDouble();
    System.out.print("Enter the number of years: ");
    double numOfYears = CONSOLE.nextDouble();

    //calculate future value of account
    double fvAccount = compoundInterest(presentValue);

    //calculate future value of annuity

    //print everything
    System.out.println("Fake Variable is " + presentValue);
    System.out.println("Future value of account is " + fvAccount);
  }
}

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

public double getWhateverResult(double presentValue, double interestRate, double numOfYears)  {
   double doubleResult = //do stuff
   return  doubleResult;
}

调用它
double doubleResult = getWhateverResult(presentValue, interestRate, numOfYears);
System.out.println(doubleResult);