我只收到一个错误,并且似乎无法修复此错误,这就是错误 您购买的是: 牛仔裤 我们有 12 双库存。 牛仔裤的价格是: $59.95 你买多少钱?:3
***** 线程“main”java.lang.Error 中出现异常: Unresolved 编译问题: RetailItem 无法解析为变量
public class CashRegisterapp
{
private final double TAX_RATE = 0.06;
private double retail;
private int quantity;
private RetailItem item;
public CashRegisterapp(RetailItem item, int q)
{
RetailItem = item;
quantity = q;
}
public double getSubtotal()
{
return retail * getSubtotal();
}
public double getTax()
{
return getSubtotal() * TAX_RATE;
}
public double getTotal()
{
return getSubtotal() + getTax();
}
}
*****下面的测试代码*****
import java.util.Scanner;
public class HW5
{
public static void main(String[] args)
{
final int unitsOnHand = 12;
final double price = 59.95;
String description = "Jeans";
int quantity;
RetailItem item = new RetailItem(description,unitsOnHand,price);
System.out.print("You are buying: "+ description+ "\nWe have "+ unitsOnHand+ " pair In stock.\nThe price of the jeans are: $"+ price+ "\n");
System.out.print("How much are you buying?: ");
Scanner keyboard = new Scanner(System.in);
quantity = keyboard.nextInt();
CashRegisterapp reg = new CashRegisterapp(item, quantity);
System.out.printf("The subtotal is: $%,.2f\n", reg.getSubtotal());
System.out.printf("The sale tax is: $%,.2f\n", reg.getTax());
System.out.printf("The total is: $%,.2f\n", reg.getTotal());
}
}
最佳答案
您不小心在 CashRegisterapp
构造函数的第一行中写入了 RetailItem
而不是 this.item
。大多数编辑器应该在您键入时突出显示此类问题。
public CashRegisterapp(RetailItem item, int q)
{
this.item = item;
this.quantity = q;
}
此外,此代码还存在其他问题:
- 您的
getSubtotal()
递归调用自身,这将导致堆栈溢出 - 您没有初始化
零售
关于java - 错误: Unresolved compilation problem: RetailItem cannot be resolved to a variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60481265/