我是一名学生,刚刚学习类和方法。我收到错误(第 30 行 - saving = pr *discount/100)“找不到符号”。我知道我的变量折扣超出了范围,但我不知道如何纠正这个问题。我已按照提供给我的说明进行操作,但它仍然不起作用。我已经在课本上发现了一些错别字,难道还漏掉了什么吗?或者是我大括号的位置?
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.");
}
}
最佳答案
您的代码是正确的 - 当您需要范围内变量 dscnt 时,您只是调用了范围外变量折扣。试试这个:
public static double computeDiscountInfo(double pr, double dscnt) {
double savings;
savings = pr * dscnt / 100;
return savings;
}
关于JAVA变量超出范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46256029/