返回方法的问题

时间:2011-11-05 17:32:26

标签: java return

我遇到了返回main方法的方法的问题。据说“返还金额”中的金额无法解析为变量。我在哪里这个?

这是我收到的消息: 此行有多个标记      - 无效方法无法返回      值      - 金额无法解决      变量

以下是代码:

import java.util.Scanner;

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

        System.out.print("Enter the amount invested: ");
        double amount = input.nextDouble();

        System.out.print("Enter the annual interest rate: ");
        double interest = input.nextDouble();

        int years = 30;

        System.out.print(futureInvestmentValue(amount, interest, years)); //Enter output for table
    }

    public static double futureInvestmentValue(double amount, double interest, int years) {

        double monthlyInterest = interest/1200;
        double temp;
        double count = 1;

        while (count < years)
            temp = amount * (Math.pow(1 + monthlyInterest,years *12));
        amount = temp;

        System.out.print((count + 1) + " " + temp);
    }

    {
        return amount;
    }
}

3 个答案:

答案 0 :(得分:0)

花括号不正确。编译器 - 和我 - 对此感到困惑。

这应该有效(至少在语法上):

import java.util.Scanner;

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

        System.out.print("Enter the amount invested: ");
        double amount = input.nextDouble();

        System.out.print("Enter the annual interest rate: ");
        double interest = input.nextDouble();

        int years = 30;

        System.out.print(futureInvestmentValue(amount, interest, years));
    }

    public static double futureInvestmentValue(
            double amount, double interest, int years) {

        double monthlyInterest = interest / 1200;
        double temp = 0;
        double count = 1;

        while (count < years)
            temp = amount * (Math.pow(1 + monthlyInterest, years * 12));
        amount = temp;

        System.out.print((count + 1) + " " + temp);

        return amount;
    }
}

答案 1 :(得分:0)

从自己的范围中删除amount作为开始。同样来自方法futureInvestmentValue,您将amount作为参数接受,但是值永远不会被修改,因此您返回的是传递的相同值,这很可能不是期望的结果。

答案 2 :(得分:0)

  1. 从自己的范围中删除return amount
  2. 方法futureInvestmentValue ...您无法修改方法内的任何参数,因此您必须在方法内声明另一个变量(可能是您继续使用的temp变量)并返回
  3. 当你返回一些东西时,return语句总是在方法内。在它自己的牙箍内部从未在它外面(从未见过它......)

    import java.util.Scanner;
    
    public class Investment {
        public static void main(String[]args) {
            Scanner input = new Scanner(System.in);
    
            System.out.print("Enter the amount invested: ");
            double amount = input.nextDouble();
    
            System.out.print("Enter the annual interest rate: ");
            double interest = input.nextDouble();
    
            int years = 30;
    
            System.out.print(futureInvestmentValue(amount, interest, years)); //Enter output for table
        }
    
        public static double futureInvestmentValue(double amount, double interest, int years) {
    
            double monthlyInterest = interest/1200;
            double temp;
            double count = 1;
    
            while (count < years) {
                temp = amount * (Math.pow(1 + monthlyInterest,years *12));
                System.out.print((count + 1) + " " + temp);
            }
    
            return amount;
        }
    }