[英]How do I use variables in one method in an equation in another method?
因此,我正在為學校做這個項目,我覺得這真的很簡單,但是我真的很傻。 我需要兩種方法; 一個僅執行我要執行的計算,另一個則在其中用戶輸入數字並顯示計算方法的答案。 該類網站上的示例使您易於理解如何使用一個用戶輸入的變量,但是卻不能像我需要的那樣使用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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.