gpt4 book ai didi

Java错误: Cannot find symbol/Inventory Program

转载 作者:行者123 更新时间:2023-12-02 07:38:43 26 4
gpt4 key购买 nike

我是 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、字符串、int、 double 发现:没有参数 原因:实际参数列表和形式参数列表的长度不同

第 29 行:错误:类型不兼容 库存[数量] = 数量+1 ^ 需要:相机 发现:int

第 31、32、33、34、35 行:错误找不到符号 System.out.printf(......) [count].getprodNumber ^ 符号:方法 getprodNumber() 位置:相机类

这是我更新的代码:

//Inventory.java

//相机库存程序 导入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

最佳答案

看来您的 Camera 类确实应该称为 Inventory。您在主应用程序类中将 Camera 用作变量名称而不是类型。

相机类更改为库存类

public Camera ( int number, String name, int total, double price)

public Inventory ( int number, String name, int total, double price)

它应该会让你更接近,即使不是一直到那里。

关于Java错误: Cannot find symbol/Inventory Program,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11857224/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com