2011-08-27 61 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