我是 Java 新手,正在尝试编译这个清单程序。我不断收到相同的错误消息,无法弄清楚我错过了什么。错误是找不到符号,它在第 10、18、20、21、22、23 行任何说 Inventory 有指向它们的 ^ 符号。我附上了我绞尽脑汁并尽我所能尝试的一切,我将不胜感激。
//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(); 必需:int、String、int、double 找到:无参数 原因:实际参数列表和形式参数列表的长度不同
第 29 行:错误:不兼容的类型 Inventory[count] = count+1 ^ required:Camera found: int
第 31、32、33、34、35 行:错误找不到符号 System.out.printf(......) [count].getprodNumber ^ 符号:方法 getprodNumber() 位置:类 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