Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在另一种方法的等式中使用一种方法中的变量?_Java_Variables_Methods_Parameters_Console - Fatal编程技术网

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

Java 如何在另一种方法的等式中使用一种方法中的变量?,java,variables,methods,parameters,console,Java,Variables,Methods,Parameters,Console,所以,我正在为学校做这个项目,我觉得它很简单,但我真的很笨。我需要有两种方法;一个只做我想做的计算,另一个用户输入数字并显示计算方法的答案。来自班级网站的示例使我们很容易理解如何使用一个用户输入的变量,而不是我需要的3个变量;我还需要能够将它们相乘,并将它们用于等式中。以下是我目前掌握的情况: import java.util.*; import java.text.*; public class Lab3StarterCodeExperimental { public static fi

所以,我正在为学校做这个项目,我觉得它很简单,但我真的很笨。我需要有两种方法;一个只做我想做的计算,另一个用户输入数字并显示计算方法的答案。来自班级网站的示例使我们很容易理解如何使用一个用户输入的变量,而不是我需要的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);
  }
}
非常感谢您的帮助

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);

非常感谢,这太棒了: