Java库存程序问题
我有2个错误消息,我无法弄清楚它要求我做什么。 错误是:
DesktopInventory ProgramInventoryPart1.java:93: cannot find symbol symbol : variable printer location: class InventoryPart1 System.out.println("Item Number: " + printer.getItemNumber()); DesktopInventory ProgramInventoryPart1.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)); } }
对于这个网站来说很抱歉,并且仍然试图通过输入整个代码来解决问题,
啊,你没有声明一个叫做printer
的变量。 你称它为epson
。
您的InventoryPart1类存在一些问题,直到我在main的开始初始化变量时才会编译。 这是一个很好的习惯,也需要使用'epson'而不是'printer':
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));
}
}
几个问题...
您需要重新考虑单位,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
链接地址: http://www.djcxy.com/p/68219.html