I am a student, just learning about Class and Methods. I am receiving an error (line 30 - savings = pr * discount / 100) "Cannot find symbol". I understand that my variable discount, is out of scope, but I cannot figure out how to correct this. I have followed the instructions provided to me, but it still is not working. I have already caught a few typos in the textbook, so is there something missing? Or is it my positioning of curly brackets?
import java.util.Scanner; // Allows for user input
public class ParadiseInfo2
{
public static void main(String[] args)
{
double price; // Variable for minimum price for discount
double discount; // Variable for discount rate
double savings; // Scanner object to use for keyboard input
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter cutoff price for discount >> ");
price = keyboard.nextDouble();
System.out.print("Enter discount rate as a whole number >> ");
discount = keyboard.nextDouble();
displayInfo();
savings = computeDiscountInfo(price, discount);
System.out.println("Special this week on any service over " + price);
System.out.println("Discount of " + discount + " percent");
System.out.println("That's a savings of at least $" + savings);
}
public static double computeDiscountInfo(double pr, double dscnt)
{
double savings;
savings = pr * discount / 100;
return savings;
}
public static void displayInfo()
{
System.out.println("Paradise Day Spa wants to pamper you.");
System.out.println("We will make you look good.");
}
}
Your code is correct - you just called the out of scope variable discount when you needed the in scope variable dscnt. Try this:
public static double computeDiscountInfo(double pr, double dscnt) {
double savings;
savings = pr * dscnt / 100;
return savings;
}
The problem is caused by, as you mentioned in your question, the variable discount
being out of scope. Let's look at why.
In your original code, the method computeDiscountInfo(double pr, double dscnt)
is passed to parameters: a double labeled pr, and another double labeled dscnt. your method will only have knowledge of these two parameters, and not of anything going on outside of it. (There are some exceptions to this, such as 'static' variables, or variables passed from a parent. However, these are most likely beyond the scope of your learning at the moment. I'm sure you wil cover them in school soon.)
Since the variable discount
is declared in the main()
method, there is no way for your computeDiscountInfo(double pr, double dscnt)
method to know of it's existence. When you cal on this method, you can pass it the discount
variable as a parameter to be used (as you do in your code with savings = computeDiscountInfo(price, discount);
) The method will then apply the value of discount
to it's own local variable dscnt
that you defined in the method's declaration. This is the variable that the method will know and use.
Now, lets look back at your method:
public static double computeDiscountInfo(double pr, double dscnt)
{
double savings;
savings = pr * discount / 100;
return savings;
}
In this method you refer to the variable as discount
, not by the local name dscnt
as declared in the method's parameters. The method has no understanding of what discount
means. It could be passed any double in this place. By changing the word discount
to dscnt
inside your method, the method will be able to understand what you are reffering to and use the value properly.
public static double computeDiscountInfo(double pr, double dscnt)
{
double savings;
savings = pr * dscnt/ 100;
return savings;
}
I hope that this makes sense to you, please let me know if it does not. The concepts of local variables and variable scope is key parts of the foundation of Object-Oriented Programming.
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.