简体   繁体   中英

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();
         
         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.

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM