在Java中遇到了一些关于return的问题。

huangapple go评论72阅读模式
英文:

having some trouble with return in java

问题

import java.util.Scanner;

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

      System.out.println("Enter current year: ");
      int CurrentYear = scan.nextInt();

      System.out.println("Enter birth year: ");
      int BirthYear = scan.nextInt();

      double PremiumAmount = calculatePremium(CurrentYear, BirthYear);
      System.out.println("Premium amount is: $" + PremiumAmount);
   }

   public static double calculatePremium(int CurrentYear, int BirthYear) {
      int decade = (CurrentYear - BirthYear) / 10;
      double PremiumAmount = (decade + 15) * 20;
      return PremiumAmount;
   }
}
英文:
import java.util.Scanner;

public class Insurance
{
   public static void main(String[] args)
      {
         Scanner scan = new Scanner(System.in);
         
         System.out.println("Enter current year: ");
         int CurrentYear = scan.nextInt();
        
         System.out.println("Enter birth year: ");
         int BirthYear = scan.nextInt();
         
         System.out.println("Premium amount is: $" + PremiumAmount);
         
         calculatePremium(CurrentYear, BirthYear);
       }
         public static int calculatePremium(int CurrentYear, int BirthYear)
            {
               int decade = (CurrentYear - BirthYear)/10;
               
               double PremiumAmount = (decade +15)*20;
               
               return PremiumAmount;
             }
        }

error: can't find symbol PremiumAmount.
System.out.println("Premium amount is: $ "+ PremiumAmount);
^

thanks in advance, and sorry if this is a silly question.

答案1

得分: 0

PremiumAmount仅在calculatePremium方法范围内可用。在main方法中无法访问。
请在main方法中修改您的代码:

        System.out.println("保费金额为:$" + calculatePremium(当前年份, 出生年份));
英文:

The PremiumAmount is available only within the scope of calculatePremium method.Its not accessible within main method.
Please modiy your code in main method:

    System.out.println("Premium amount is: $" + calculatePremium(CurrentYear, BirthYear));

答案2

得分: 0

你尚未陈述你的问题,但从我所见,我建议你查看此网站以找出问题的原因。

“找不到符号”错误通常在您尝试引用代码中未声明的变量时发生。 “找不到符号”错误意味着编译器无法执行此操作。您的代码似乎在引用编译器不理解的内容。

你没有在主函数中声明PremiumAmount,你只在calculatePremium中声明了它,这在主函数中无法访问。由于calculatePremium方法返回PremiumAmount的值,您可以尝试像这样放置它

System.out.println("保费金额为:$" + calculatePremium());

并且不要忘记删除其下面的那一行。

英文:

You haven't stated your question yet, but from what I see, I can advise you to take a look at this site to find out the cause to your problem
> The "Cannot find symbol" errors generally occur when you try to reference an undeclared variable in your code. A "Cannot find symbol" error means that the compiler cannot do this. Your code appears to be referring to something that the compiler doesn't understand.

You haven't declared your PremiumAmount in the main function, you only declared it in the calculatePremium, which cannot be accessed from the main. Since the calculatePremium method returns the value for PremiumAmount, you can try putting it like this

System.out.println("Premium amount is: $" + calculatePremium());

And don't forget to remove the line beneath it.

答案3

得分: 0

我建议阅读关于Java中的“作用域”(Scope)。 (https://www.w3schools.com/java/java_scope.asp)
您之所以会得到那个错误是因为PremiumAmount变量只存在于第二个方法的作用域中。您还需要将calculatePremium方法返回int更改为double。我已经修复了这些错误,并进行了缩进。尽量保持方法分开,这有助于理解复杂的代码。

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

    System.out.println("Enter current year: ");
    int CurrentYear = scan.nextInt();

    System.out.println("Enter birth year: ");
    int BirthYear = scan.nextInt();

    double PremiumAmount = calculatePremium(CurrentYear, BirthYear);

    System.out.println("Premium amount is: $" + PremiumAmount);
}

public static double calculatePremium(int CurrentYear, int BirthYear) {
    int decade = (CurrentYear - BirthYear) / 10;

    double PremiumAmount = (decade + 15) * 20;

    return PremiumAmount;
}
英文:

I recommend reading up on "Scope" in Java. (https://www.w3schools.com/java/java_scope.asp)
You're getting that error because the PremiumAmount variable only exists in the scope of the second method. You also have the calculatePremium method returning an int, when it needs to be a double. I fixed those errors, and the indentation. Try to keep methods separated, it helps understand complicated code.

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

    System.out.println("Enter current year: ");
    int CurrentYear = scan.nextInt();

    System.out.println("Enter birth year: ");
    int BirthYear = scan.nextInt();

    double PremiumAmount = calculatePremium(CurrentYear, BirthYear);

    System.out.println("Premium amount is: $" + PremiumAmount);

}

public static double calculatePremium(int CurrentYear, int BirthYear) {
    int decade = (CurrentYear - BirthYear)/10;

    double PremiumAmount = (decade +15)*20;

    return PremiumAmount;
}

huangapple
  • 本文由 发表于 2020年10月6日 11:40:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/64219021.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定