【问题标题】:How do I use variables in one method in an equation in another method?如何在另一种方法的方程式中使用一种方法中的变量?
【发布时间】:2014-03-28 10:45:15
【问题描述】:

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

非常感谢任何帮助!

【问题讨论】:

    标签: java variables methods parameters console


    【解决方案1】:
    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);
    

    【讨论】:

    • 非常感谢,这太棒了:)
    猜你喜欢
    • 2012-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-25
    • 1970-01-01
    • 2020-06-18
    相关资源
    最近更新 更多