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