Java Inventory Program problems
我有两条错误消息,我很难弄清楚它在向我请求什么。错误是:
1 2 3 4 5 6 7 8 9 | 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()); |
迄今为止的准则是
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | 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)); } } |
抱歉,这是一个新网站,但仍在努力解决输入整个代码的问题,
啊,您没有声明一个名为
您的inventorypart1类有几个问题,在我在main开头初始化变量之前,它不会为我编译。这是一个很好的习惯,而且你需要使用"epson"而不是"printer"。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | 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)); } } |
几个问题…
- 实例名应该是打印机,而不是epson
- 单位总数应为静态字段而不是实例变量
- 要么在使用之前初始化所有局部变量,要么删除它们,因为您可以使用打印机类中的成员方法计算统计信息。
- 显示时使用打印机类中的所有成员方法而不是局部变量
- 使用numberFormat.getIntegerInstance将整数格式化为numberFormat.getCurrencyInstance用于格式化货币字段。
您需要重新考虑Units、UnitsTotal、Units Price和Inventory字段。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | 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())); } } |
样品运行:
1 2 3 4 5 6 7 |