简体   繁体   English

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

[英]How do I use variables in one method in an equation in another method?

So, I'm doing this project for school, and I have a feeling it's really simple, but that I'm just really dumb. 因此,我正在为学校做这个项目,我觉得这真的很简单,但是我真的很傻。 I need to have two methods; 我需要两种方法; one that only does the calculations I want done, and another where the user enters in numbers and displays the answers from the calculations method. 一个仅执行我要执行的计算,另一个则在其中用户输入数字并显示计算方法的答案。 The sample from the class website makes it easy to understand how to use one user-entered variable, but not 3 like I need; 该类网站上的示例使您易于理解如何使用一个用户输入的变量,但是却不能像我需要的那样使用3个变量。 I also need to be able to multiply them all together and use them in an equation. 我还需要能够将它们全部相乘并在方程式中使用它们。 Here's what I have so far: 这是我到目前为止的内容:

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

Any help is greatly appreciated! 任何帮助是极大的赞赏!

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

Call it with 叫它

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM