// Inventory.java part 1
// this program is to calculate the value of the inventory of the Electronics Department's cameras
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class Inventory
{
public void main(String[] args)
{
// create Scanner to obtain input from the command window
Scanner input = new Scanner (System.in);
int itemNumber; // first number to multiply
int itemStock; // second number to multiply
double itemPrice; //
double totalValue; // product of number1 and number2
while(true){ // infinite loop
// make new Camera object
Cam camera = new Cam(name, itemNumber, itemStock, itemPrice, totalValue);
System.out.print("Enter Department name: "); //prompt
String itemDept = input.nextLine(); // read name from user
if(itemDept.equals("stop")) // exit the loop
break;
while(true){
System.out.print("Enter item name: "); // prompt
String name = input.nextLine(); // read first number from user
input.nextLine();
if(name != ("camera"))
System.out.print("Enter valid item name:"); // prompt
name = input.nextLine(); // read first number from user
input.nextLine();
break;
}
System.out.print("Enter number of items on hand: "); // prompt
itemStock = input.nextInt(); // read first number from user
input.nextLine();
while( itemStock <= -1){
System.out.print("Enter positive number of items on hand:"); // prompt
itemStock = input.nextInt(); // read first number from user
input.nextLine();
} /* while statement with the condition that negative numbers are entered
user is prompted to enter a positive number */
System.out.print("Enter item Price: "); // prompt
itemPrice = input.nextDouble(); // read second number from user
input.nextLine();
while( itemPrice <= -1){
System.out.print("Enter positive number for item price:"); // prompt
itemPrice = input.nextDouble(); // read first number from user
input.nextLine();
} /* while statement with the condition that negative numbers are entered
user is prompted to enter a positive number */
totalValue = itemStock * itemPrice; // multiply numbers
System.out.println("Department name:" + itemDept); // display Department name
System.out.println("Item number: " + camera.getItemNumber()); //display Item number
System.out.println("Product name:" + camera.getName()); // display the item
System.out.println("Quantity: " + camera.getItemStock());
System.out.println("Price per unit" + camera.getItemPrice());
System.out.printf("Total value is: $%.2f\n", camera.getTotalValue()); // display product
} // end while method
} // end method main
}/* end class Inventory */
class Cam{
private String name;
private int itemNumber;
private int itemStock;
private double itemPrice;
private String deptName;
private Cam(String name, int itemNumber, int itemStock, double itemPrice, double totalValue) {
this.name = name;
this.itemNumber = itemNumber;
this.itemStock = itemStock;
this.itemPrice = itemPrice;
this.totalValue = totalValue;
}
public String getName(){
return name;
}
public double getTotalValue(){
return itemStock * itemPrice;
}
public int getItemNumber(){
return itemNumber;
}
public int getItemStock(){
return itemStock;
}
public double getItemPrice(){
return itemPrice;
}
}
这是我尝试编译此代码时的输出:
C:\Java>javac Inventory.java
Inventory.java:25: error: cannot find symbol
Cam camera = new Cam(name, itemNumber, itemStock, itemPrice, totalValue);
^
symbol: variable name
location: class Inventory
Inventory.java:98: error: cannot find symbol
this.totalValue = totalValue;
^
symbol: variable totalValue
2 errors
我不明白为什么我会一直收到这些错误。我觉得我已接近完成问题,但发现我需要帮助解决这个问题。
好的,我做了一些更改,但现在我收到了这些错误:
C:\Java>javac Inventory.java
Inventory.java:68: error: variable itemNumber might not have been initialized
Cam camera = new Cam(name, itemNumber, itemStock, itemPrice, totalValue);
^
Inventory.java:68: error: variable totalValue might not have been initialized
Cam camera = new Cam(name, itemNumber, itemStock, itemPrice, totalValue);
^
2 errors
答案 0 :(得分:1)
您已在 Inventory 类的主要功能中声明了变量 totalValue 。它不能用于 Cam 类作为实例(此)变量。
答案 1 :(得分:1)
您在main中的变量name
缺少声明。
// create Scanner to obtain input from the command window
Scanner input = new Scanner (System.in);
String name; //missing declaration
int itemNumber; // first number to multiply
int itemStock; // second number to multiply
double itemPrice; //
double totalValue; // product of number1 and number2
此外:
class Cam{
private String name;
private int itemNumber;
private int itemStock;
private double itemPrice;
private String deptName;
private double totalValue; //missing field
private Cam(String name, int itemNumber, int itemStock, double itemPrice, double totalValue) {
构造函数是私有的。
您的主要方法中也遗漏了static
。
答案 2 :(得分:1)
这两个错误非常简单:
首先:
Inventory.java:25: error: cannot find symbol Cam camera = new Cam(name, itemNumber, itemStock, itemPrice, totalValue); ^ symbol: variable name location: class Inventory
这个sais找不到名为name
的变量,这是真的。当您尝试构造Cam
时,范围中没有名称变量。我们知道你有一个name
变量,但这不起作用,因为该变量不在new
语句的范围内。
第二
Inventory.java:98: error: cannot find symbol this.totalValue = totalValue; ^ symbol: variable totalValue
它在totalValue
类中找不到名为Cam
的值,这也是正确的。查看Cam
的成员列表,您会看到没有totalValue
。我想你想从构造函数中删除它,因为你是根据itemStock
和itemPrice
来计算总价值。
注意:
如果你解决了这个问题(可能还有更多的编译错误),你会注意到你的应用程序将编译,但不会运行。这是因为您忘记申报main
- 方法static
。
如果您已经解决了这个问题,您会注意到您构建的所有Cam
个对象都将包含您为之前的Cam
输入的数据。这是因为您在提示数据之前构建了Cam
。您开始使用:声明要提示的数据的字段。当用户输入一个相机的所有数据时,构建Camera
。
答案 3 :(得分:0)
您的文件中有两个类
您需要定义
name
totalValue
您在Inventory.java中对Cam
构造函数的调用似乎处于错误的位置,您可能希望将其作为While
循环的最后一行