如何返回主方法?

时间:2012-02-29 02:54:40

标签: java

我需要帮助找出如何从method1获取Fvalue1并将其返回到main方法。

import java.util.*;

public class FutureValues {
    public static void main(String[] args) {
        Scanner CONSOLE = new Scanner(System.in);

        System.out.print("present value  : ");
        double Pvalue = CONSOLE.nextDouble();
        System.out.print("interest rate  : ");
        double Interest = CONSOLE.nextDouble();
        System.out.print("number of years : ");
        double Years = CONSOLE.nextDouble();

        method1(Pvalue, Interest, Years);

        System.out.print("The future value using compound interest = " + Fvalue1);

        return Fvalue1;
    }

    public static double method1(double Pvalue, double Interest, double Years) {
        return Fvalue1 = Pvalue * Math.pow(1 + Interest / 100, Years);
    }
}

1 个答案:

答案 0 :(得分:0)

您正在错误地返回答案,而不是在调用函数中设置答案。这应该有效:

import java.util.*;

public class FutureValues {

    public static void main(String[] args) {
        Scanner CONSOLE = new Scanner(System.in);

        System.out.print("present value   : ");
        double Pvalue = CONSOLE.nextDouble();
        System.out.print("interest rate   : ");
        double Interest = CONSOLE.nextDouble();
        System.out.print("number of years : ");
        double Years = CONSOLE.nextDouble();

        double Fvalue1 = method1(Pvalue, Interest, Years);

        System.out.print("The future value using compound interest = " + Fvalue1);
    }

    public static double method1(double Pvalue, double Interest, double Years) {
        return Pvalue * Math.pow(1 + Interest / 100, Years);
    }
}

主要变化是:

double Fvalue1 = method1(Pvalue, Interest, Years);

return Pvalue * Math.pow(1 + Interest / 100, Years);