java - 错误: Unresolved compilation problem: RetailItem cannot be resolved to a variable

标签 java

我只收到一个错误,并且似乎无法修复此错误,这就是错误 您购买的是: 牛仔裤 我们有 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/

相关文章:

java - 在 Java 中从环境接收打开的套接字

Java - ATM 银行登录,尝试 3 次密码锁

java - 使用 Reactive Streams 的 Reactive Pull-Based BackPressure

java - RSA 加密在 JavaCard 上间歇性地抛出异常

Java - 如何在特定条件下获得最佳元素列表

java - 用java读取hex文件并将其转换为ascii

java - 为什么来自BluetoothSocket的输入/输出流被评估为NOT null,然后抛出空指针异常?

java - 子表行的 Hibernate 批处理

java - 导出 MySQL 数据库并在 *.jar 文件中使用它

java - Google map 未找到类 "com.google.android.gms.dynamic.zza"