Java库存计划问题

时间:2011-08-27 20:45:15

标签: java

我有2条错误消息,我无法弄清楚它的要求是什么。错误是:

Desktop\Inventory Program\InventoryPart1.java:93: cannot find symbol
symbol  : variable printer
location: class InventoryPart1
        System.out.println("Item Number: " + printer.getItemNumber());

Desktop\Inventory Program\InventoryPart1.java:95: cannot find symbol
symbol  : variable printer`enter code here`
location: class InventoryPart1
        System.out.println("Product Name: " + printer.getProductName());

到目前为止的代码是

import java.text.NumberFormat;
import java.util.locale;
import java.util.Scanner;

class Printer {

    private String itemNumber;
    private String productName;
    private double units;
    private double unitPrice;
    private double unitsTotal;
    //constructor
    public Printer (String itemNumber, String productName, double units, double unitsTotal) {
        setItemNumber(itemNumber);
        setProductName(productName);
        setUnits(units);
        setUnitPrice(unitPrice);
        unitsTotal = units ++;
    }

    //accessor methods for class variables
    public String getItemNumber () {
        return itemNumber;
    }

    public void setItemNumber (String itemNumber) {
        this.itemNumber = itemNumber;
    }

    public String getProductName () {
        return productName;
    }

    public void setProductName (String productName) {
        this.productName = productName;
    }

    public double getUnits () {
        return units;
    }

    public void setUnits (double units) {
        this.units = units;
    }

    public double getUnitPrice () {
        return unitPrice;
    }

    public void setUnitPrice (double unitPrice) {
        this.unitPrice = units * unitPrice;
    }

    public double getUnitsTotal () {
        return unitsTotal;
    }

    public void setUnitsTotal (double unitsTotal) {
        this.unitsTotal = units ++;
    }


}

public class InventoryPart1 {

    public static void main (String args[]) {


        int units;

        double unitPrice;

        double unitsTotal;
        unitsTotal = units ++;

        double unitsPrice;
        unitsPrice = units * unitPrice;

        double unitsTotalPrice;
        unitsTotalPrice = unitsTotal * unitPrice;

        double totalInventory;
        totalInventory = unitsTotal * unitsTotalPrice;


        NumberFormat nf = NumberFormat. getCurrencyInstance(Locale.US);

        //create an instance of the Printer class
        Printer epson = new Printer ("Epson579", "All In One", 2, 50.99);

        //use the methods from class Printer to output the inventory details.
        System.out.println("Item Number: " + printer.getItemNumber());

        System.out.println("Product Name: " + printer.getProductName());

        System.out.print("Number of Units: ");
        System.out.println(nf.format(units));

        System.out.print("Unit Price: ");
        System.out.println(nf.format(unitPrice));

        System.out.print("Units Total: ");
        System.out.println(nf.format(unitsTotal));

        System.out.print("Units Total Price: ");
        System.out.println(nf.format(unitsTotalPrice));

        System.out.print("Total Inventory: ");
        System.out.println(nf.format(totalInventory));
    }

}

对不起该网站的新用户,并且仍在努力解决整个代码输入问题,

3 个答案:

答案 0 :(得分:5)

啊,你没有声明一个名为printer的变量。你称之为epson

答案 1 :(得分:3)

您的InventoryPart1类存在一些问题,在我在main的开头初始化变量之前,它不会为我编译。这是一个很好的习惯,你需要使用'epson'而不是'打印机':

public class InventoryPart1 {

    public static void main (String args[]) {


        int units = 0;

        double unitPrice = 0;

        double unitsTotal = units++;

        double unitsPrice = 0;
        unitsPrice = units * unitPrice;

        double unitsTotalPrice;
        unitsTotalPrice = unitsTotal * unitPrice;

        double totalInventory;
        totalInventory = unitsTotal * unitsTotalPrice;


        NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);

        //create an instance of the Printer class
        Printer epson = new Printer ("Epson579", "All In One", 2, 50.99);

        //use the methods from class Printer to output the inventory details.
        System.out.println("Item Number: " + epson.getItemNumber());

        System.out.println("Product Name: " + epson.getProductName());

        System.out.print("Number of Units: ");
        System.out.println(nf.format(units));

        System.out.print("Unit Price: ");
        System.out.println(nf.format(unitPrice));

        System.out.print("Units Total: ");
        System.out.println(nf.format(unitsTotal));

        System.out.print("Units Total Price: ");
        System.out.println(nf.format(unitsTotalPrice));

        System.out.print("Total Inventory: ");
        System.out.println(nf.format(totalInventory));
    }
}

答案 2 :(得分:0)

一些问题......

  • 实例名称应为打印机而不是epson
  • 单位总数不应该是静态字段而不是实例变量
  • 在使用之前初始化所有局部变量或删除它们,因为您可以使用Printer类中的成员方法计算统计数据
  • 显示时使用Printer类的所有成员方法而不是局部变量
  • 使用NumberFormat.getIntegerInstance格式化整数,因为NumberFormat.getCurrencyInstance用于格式化货币字段。

您需要重新考虑单位,单位总价,单位价格和库存字段。

import java.text.NumberFormat;
import java.util.Locale;

class Printer {

    private String itemNumber;
    private String productName;
    private double units;
    private double unitPrice;
    private static double unitsTotal;

    // constructor
    public Printer(String itemNumber, String productName, double units, double unitPrice) {
        setItemNumber(itemNumber);
        setProductName(productName);
        setUnits(units);
        setUnitPrice(unitPrice);
        unitsTotal += units;
    }

    // accessor methods for class variables
    public String getItemNumber() {
        return itemNumber;
    }

    public void setItemNumber(String itemNumber) {
        this.itemNumber = itemNumber;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public double getUnits() {
        return units;
    }

    public void setUnits(double units) {
        this.units = units;
    }

    public double getUnitPrice() {
        return unitPrice;
    }

    public void setUnitPrice(double unitPrice) {
        this.unitPrice = unitPrice;
    }

    public static double getUnitsTotal() {
        return unitsTotal;
    }
}

public class InventoryPart1 {

    public static void main(String args[]) {
        NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
        NumberFormat inf = NumberFormat.getIntegerInstance(Locale.US);

        // create an instance of the Printer class
        Printer printer = new Printer("Epson579", "All In One", 2, 50.99);

        // use the methods from class Printer to output the inventory details.
        System.out.println("Item Number      : " + printer.getItemNumber());
        System.out.println("Product Name     : " + printer.getProductName());
        System.out.println("Number of Units  : " + inf.format(printer.getUnits()));
        System.out.println("Unit Price       : " + nf.format(printer.getUnitPrice()));
        System.out.println("Units Total      : " + inf.format(printer.getUnitsTotal()));
        System.out.println("Units Total Price: " + nf.format(printer.getUnitPrice() * Printer.getUnitsTotal()));
        System.out.println("Total Inventory  : " + inf.format(Printer.getUnitsTotal()));
    }
}

示例运行:

Item Number      : Epson579
Product Name     : All In One
Number of Units  : 2
Unit Price       : $50.99
Units Total      : 2
Units Total Price: $101.98
Total Inventory  : 2