2011-08-27 39 views
0

我有2个错误消息,我无法弄清楚它是什么问我。的错误是:Java库存程序问题

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)); 
    } 

}

对不起新来这个网站,并仍在试图搞清楚这些事情与整个代码进入,

+0

这一切都很好!我为你格式化了代码,所以不用担心。 :) – fireshadow52

回答

2

啊,你没声明一个名为printer的变量。你叫它epson

1

您的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)); 
    } 
} 
+0

谢谢我将尝试做出这些变化,看看会发生什么。该帮助非常感谢 –

+0

它在javac下工作,但是当我点击运行java applet时,它不会初始化。当我点击java它给了我这个错误信息:找不到主要类:C:\ Users \ Colby和Erin \ Desktop \ Inventory Program \ InventoryPart1.java。程序将会退出。 线程“主”中的异常 工具完成退出代码1 –

+0

嗯,这是一个单独的问题,也许你可以发布更多的细节,例如我不知道你是什么意思'我点击运行java applet',从哪里?在ide里面?还要确保你的课程是公开的,打印机不是(所以它包装可见),那是你想要的? – eon

0

几个问题...

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

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

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