找不到符号 - class InventoryItem

Cannot find symbol - class InventoryItem

我从书中重新输入了这些代码,但不知何故我得到了错误“找不到符号 - class InventoryItem”

import java.util.Scanner;

public class ReturnObject {    
    public static void main(String[] args) {
        InventoryItem item;
        item = getData();

        System.out.println("Des: " + item.getDescription() + " Unit: " + 
        item.Units());

    }

    public static InventoryItem getData() {
        String desc;
        int units;
        Scanner keyboard = new Scanner(System.in);
        System.out.print("enter descri: ");
        desc = keyboard.nextLine();
        System.out.print("number of unit: ");
        units = keyboard.nextInt();
        return new InventoryItem(desc, units);
    }
}

我是 java 的新手,请帮忙 谢谢。

您当前所在的class找不到class(符号)InventoryItem。您需要定义此 class 和 getData 方法。

public class InventoryItem{

    private String desc;
    private int units;

    public InventoryItem(){
        Scanner keyboard = new Scanner(System.in);
        System.out.print("enter descri: ");
        desc = keyboard.nextLine();
        System.out.print("number of unit: ");
        units = keyboard.nextInt();

    }

    public static InventoryItem getData() {

        return this;
       }

    }

我认为这应该是您需要的InventoryItem

 /**
 * This class uses three constructors.
 */

public class InventoryItem {
    private String description;  // Item description
    private int units;           // Units on-hand

    /**
     * No-arg constructor
     */

    public InventoryItem() {
        description = "";
        units = 0;
    }

    /**
     * The following constructor accepts a
     * String argument that is assigned to the
     * description field.
     */

    public InventoryItem(String d) {
        description = d;
        units = 0;
    }

    /**
     * The following constructor accepts a
     * String argument that is assigned to the
     * description field, and an int argument
     * that is assigned to the units field.
     */

    public InventoryItem(String d, int u) {
        description = d;
        units = u;
    }

    /**
     * The setDescription method assigns its
     * argument to the description field.
     */

    public void setDescription(String d) {
        description = d;
    }

    /**
     * The setUnits method assigns its argument
     * to the units field.
     */

    public void setUnits(int u) {
        units = u;
    }

    /**
     * The getDescription method returns the
     * value in the description field.
     */

    public String getDescription() {
        return description;
    }

    /**
     * The getUnits method returns the value in
     * the units field.
     */

    public int getUnits() {
        return units;
    }
}

complete example click here and here

也许你的InventoryItemclass:



    public class InventoryItem {
        private String desc;
        private int units;
        public InventoryItem(String desc, int units) {
            this.desc=desc;
            this.units=units;
        }

        public String getDescription() {
            return desc;
        }

        public int Units() {
            return units;
        }
    }