【问题标题】:return issue for method方法的返回问题
【发布时间】:2011-11-05 17:32:26
【问题描述】:

我遇到了返回主方法的方法的问题。这是说“退货金额”中的金额无法解析为变量。我在哪里?

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

代码如下:

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

【问题讨论】:

  • 尝试缩进你的代码。使其更易于阅读。
  • 你不能从主方法返回一些东西。删除返回并打印一些消息或错误。
  • 请添加适当的标签并缩进您的代码。
  • 你从中学到了什么???
  • 您对代码进行的编辑使您最初的问题感到困惑。在这一点上,我不确定你在问什么。

标签: java return


【解决方案1】:

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

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

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

【讨论】:

  • 它不必在那个块中声明;外部范围是可见的。
  • 不,我认为你是对的;我误读了原始代码——它根本不在方法中。
  • 好的,正如你所指出的,我有花括号,但现在它告诉我 temp 尚未初始化。它在 while 循环中记录了这一点。我不确定这意味着什么。
  • @user1019838: temp 必须像double temp = 0; 一样初始化(获取初始值),因为它没有默认值
【解决方案2】:

从自己的作用域中删除amount 作为开始。同样从方法futureInvestmentValue 中,您将amount 作为参数,但该值从未被修改,因此您返回的值与传递的相同,这很可能不是预期的结果。

【讨论】:

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

    【讨论】:

      猜你喜欢
      • 2022-06-10
      • 1970-01-01
      • 2021-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-30
      相关资源
      最近更新 更多