我是Java新手并尝试编译此库存程序。我一直收到相同的错误信息,无法弄清楚我错过了什么。错误是找不到符号,它在第10,18,20,21,22,23行,任何说库存有^符号指向它们。我随心所欲地绞尽脑汁,尽我所能,并感谢任何帮助。
//InventoryProgram2.java
//Camera inventory program
import java.util.Arrays;
public class InventoryProgram2
{
public static void main( String args [])
{
//instantiate camera object
Inventory myInventory = new Inventory();
//displays welcome message
System.out.println( "Camera Invenotry Program");
System.out.println();//skips a line
//create and initialize an array of Cameras
Inventory[] Camera = new Inventory[4];
Camera[0] = new Inventory( 1980, "Cannon Rebel T3", 20, 489.99);
Camera[1] = new Inventory( 2120, "Nikon CoolPix L810", 5, 279.99);
Camera[2] = new Inventory( 1675, "Sony CyberShot HX200V", 12, 479.99);
Camera[3] = new Inventory( 1028, "Fujifilm FinePix S4300", 3, 199.99);
//for each array element, output value
for(int count = 0; count < Camera.length; count++)
{
Camera[count] = count+1;
System.out.printf("Product Number: %4.2f\n", Camera[count].getprodNumber() );
System.out.printf("Product Name: %s\n", Camera[count].getprodName() );
System.out.printf("Units In Stock: %.2f\n", Camera[count].getunitsTotal() );
System.out.printf("Unit Price: $%4.2f\n", Camera[count].getunitPrice() );
System.out.printf("Inventory Value: $%4.2f\n", Camera[0].gettotalInventory() );
System.out.println();//blank line to seperate products
}//end for
}//end main method
}//end public class InventoryProgram2
class Camera
{
private int prodNumber;//product number
private String prodName;//product name
private int unitsTotal;//total units in stock
private double unitPrice;//price per unit
private double totalInventory;//amount of total inventory
//initializa four-argument constructor
public Camera ( int number, String name, int total, double price)
{
prodNumber = number;
prodName = name;
setUnitsTotal (total);//validate and store total of camera
setUnitPrice (price);//validate and store price per camera
}//end four-argument constructor
public void setProdNumber (int number)
{
prodNumber = number;
}
public int getProdNumber()
{
return prodNumber;
}
public void setProdName (String name)
{
prodName = name;
}
public String getProdName()
{
return prodName;
}
public void setUnitsTotal (int total)
{
unitsTotal = total;
}
public int getUnitsTotal()
{
return unitsTotal;
}
public void setUnitPrice (double price)
{
unitPrice = price;
}
public double getUnitPrice()
{
return unitPrice;
}
// method to set Inventory value
//public void setInventoryValue(double value)
//{
//InventoryValue = value;
//}end method setInventoryValue
//method to get InventoryValue
//public double getInventoryValue()
//{
// return InventoryValue;
//} //end method to getInventoryValue
public double getInventoryValue()
{
return unitPrice * unitsTotal;
}//end method to getInventoryValue
//method to set TotalInventory
//public void setTotalInventory(double value)
//{
//TotalInventory = total;
//}end method setTotalInventory
//method to get TotalInventory
//public double getTotalInventory()
//{
//return TotalInventory;
//}end method to getTotalInventory
}//end class Camera
我需要保留一台摄像机,所以我做了一些调整。我有以下7个错误:
第10行:错误:构造函数Camera中的Camera不能应用于给定的类型; 相机myCamera = new Camera(); required:int,String,int,double 发现:没有争论 原因:实际和正式的参数列表长度不同
第29行:错误:不兼容的类型 库存[count] = count + 1 ^ 必需:相机 发现:int
第31,32,33,34,35行:错误找不到符号 System.out.printf(......)[count] .getprodNumber ^ 符号:方法getprodNumber() location:class Camera
这是我更新的代码:
//Inventory.java
//相机库存计划 import java.util.Arrays;
public class Inventory
{
public static void main( String args [])
{
//instantiate camera object
Camera myCamera = new Camera();
//displays welcome message
System.out.println( "Camera Invenotry Program");
System.out.println();//skips a line
//create and initialize an array of Cameras
Camera[] Inventory = new Camera[4];
Inventory[0] = new Camera( 1980, "Cannon Rebel T3", 20, 489.99);
Inventory[1] = new Camera( 2120, "Nikon CoolPix L810", 5, 279.99);
Inventory[2] = new Camera( 1675, "Sony CyberShot HX200V", 12, 479.99);
Inventory[3] = new Camera( 1028, "Fujifilm FinePix S4300", 3, 199.99);
//for each array element, output value
for(int count = 0; count < Inventory.length; count++)
{
Inventory[count] = count+1;
System.out.printf("Product Number: %4.2f\n", Inventory[count] .getprodNumber() );
System.out.printf("Product Name: %s\n", Inventory[count] .getprodName() );
System.out.printf("Units In Stock: %.2f\n", Inventory[count] .getunitsTotal() );
System.out.printf("Unit Price: $%4.2f\n", Inventory[count] .getunitPrice() );
System.out.printf("Inventory Value: $%4.2f\n", Inventory[0] .gettotalInventory() );
System.out.println();//blank line to seperate products
}//end for
}//end main method
}//end public class Inventory
class Camera
{
private int prodNumber;//product number
private String prodName;//product name
private int unitsTotal;//total units in stock
private double unitPrice;//price per unit
private double totalInventory;//amount of total inventory
//initializa four-argument constructor
public Camera ( int number, String name, int total, double price)
{
prodNumber = number;
prodName = name;
setUnitsTotal (total);//validate and store total of camera
setUnitPrice (price);//validate and store price per camera
}//end four-argument constructor
public void setProdNumber (int number)
{
prodNumber = number;
}
public int getProdNumber()
{
return prodNumber;
}
public void setProdName (String name)
{
prodName = name;
}
public String getProdName()
{
return prodName;
}
public void setUnitsTotal (int total)
{
unitsTotal = total;
}
public int getUnitsTotal()
{
return unitsTotal;
}
public void setUnitPrice (double price)
{
unitPrice = price;
}
public double getUnitPrice()
{
return unitPrice;
}
// method to set Inventory value
//public void setInventoryValue(double value)
//{
//InventoryValue = value;
//}end method setInventoryValue
//method to get InventoryValue
//public double getInventoryValue()
//{
// return InventoryValue;
//} //end method to getInventoryValue
public double getInventoryValue()
{
return unitPrice * unitsTotal;
}//end method to getInventoryValue
//method to set TotalInventory
//public void setTotalInventory(double value)
//{
//TotalInventory = total;
//}end method setTotalInventory
//method to get TotalInventory
//public double getTotalInventory()
//{
//return TotalInventory;
//}end method to getTotalInventory
}//end class Camera
答案 0 :(得分:2)
看起来您的Camera类应该真正称为Inventory。您在主应用程序类中使用Camera作为变量名而不是类型。
将class Camera
更改为class Inventory
和
public Camera ( int number, String name, int total, double price)
到
public Inventory ( int number, String name, int total, double price)
它应该让你更近,如果不是那么一切。
答案 1 :(得分:0)
检查Inventory
类是否与此类的包在同一个包中。否则只需像Arrays
那样导入该类。