为什么我会收到这些错误?
// 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
// 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;
}
}
This is the output when I try to compile this code:
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
I don't understand why I keep getting these errors. I feel like I am close to finishing the problem, but find that I need help getting over this bit of the problem.
Okay, I have made a few changes, but now I get these 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您已在 Inventory 类的 main 函数中声明了一个变量 totalValue。它不适用于类 Cam 作为实例 (this) 变量。
You have declared a variable totalValue inside the main function of the Inventory class. It is not available for class Cam as an instance (this) variable.
main.c 中缺少变量
name
的声明。另外:
构造函数是私有的。
您的 main 方法中还缺少
static
。Your missing declaration for the variable
name
in main.Also:
The constructor is private.
Your are also missing
static
in your main method.这两个错误非常简单:
第一:
这表示它找不到名为
name
的变量,这是事实。当您尝试构造Cam
时,作用域中没有名称变量。我们知道您还有一个name
变量,但这不起作用,因为该变量不在new
语句的范围内。第二:
它说它在
Cam
类中找不到名为totalValue
的值,这也是事实。查看Cam
的成员列表,会发现没有totalValue
。我猜您想从构造函数中删除它,因为您正在根据itemStock
和itemPrice
计算总价值。备注:
如果您解决了这个问题(可能还有更多编译错误),您会注意到您的应用程序将编译,但无法运行。这是因为您忘记声明您的
main
方法static
。如果您已经解决了这个问题,您会注意到您构造的所有
Cam
对象都将包含您为之前的Cam
输入的数据。这是因为您在提示数据之前构建了Cam
。您开始得很好:为您想要提示的数据声明字段。当用户输入一个Camera的所有数据后,构造Camera
。The two errors are pretty simple:
First:
This sais it cannot find the variable called
name
, which is true. There is no name variable in the scope when you try to construct theCam
. We know you have aname
variable a bit further, but that does not work, because the variable isn't in the scope of thenew
statement.Second:
It sais it can't find a value called
totalValue
in theCam
class, which is true as well. Check out the member list ofCam
and you will see that there is nototalValue
. I guess you want to remove it from the constructor, because you are calculating total value depending onitemStock
anditemPrice
.Notes:
If you solved this, (and probably more compilation errors) you will notice that your application will compile, but not run. This is because of you forget to declare your
main
-methodstatic
.If you have solved this you will notice that all the
Cam
objects you constructed, will contain the data you entered for the previousCam
. This is because of you are constructing theCam
before prompting the data. You started good: Declare fields for the data you want to prompt. When the user entered all the data of one Camera, construct theCamera
.文件中有两个类
您需要
name
totalValue
在 Class Cam 中另外,您还可以在 Inventory.java 中调用
Cam
构造函数似乎位于错误的位置,您可能希望将其作为While
循环的最后一行There are two classes in your file
You need to define
name
in Class InventorytotalValue
in Class CamAlso your in Inventory.java the call to
Cam
constructor seems to be at a wrong place, you might want to make it the last line ofWhile
loop