【问题标题】:Cannot find symbol - class InventoryItem找不到符号 - 类 InventoryItem
【发布时间】:2019-03-24 00:43:35
【问题描述】:

我从书中重新键入了这些代码,但不知何故我得到了错误“找不到符号 - 类 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新手,请帮忙 谢谢。

【问题讨论】:

  • InventoryItem 类在哪里定义?编译器错误似乎在抱怨这个。

标签: java syntax-error cannot-find-symbol


【解决方案1】:

您当前所在的班级找不到班级(符号)InventoryItem。您需要定义此类和 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;
       }

    }

【讨论】:

    【解决方案2】:

    我认为这应该是您需要的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 herehere

    【讨论】:

      【解决方案3】:

      也许你的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;
              }
          }
      
      

      【讨论】:

        猜你喜欢
        • 2017-10-01
        • 2018-09-28
        • 2015-03-21
        • 2018-08-09
        • 2014-06-23
        • 2017-05-07
        • 2021-08-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多