在 InventoryPart 2 程序上工作的 Java 代码,不知道我做错了什么?
我收到以下错误代码:
C:\Documents and Settings\AdminUser\My Documents\InventoryPart2.java:83:需要类、接口或枚举
导入 java.util.*;
^
1 个错误工具已完成,退出代码为 1
编译这些源文件时出现
public class Television { //class name and attributes
private String ItemNumber; //item # of product
private String ProductName; //product name
private double UnitsStock; //# of units in stock
private double UnitPrice; //Price per unit
private double InventoryValue; //The dollar value of the inventory in stock
private double CalculateInventory; //The total value of all of the inventory in stock
private double value;
//constructor
public Television (String item, String product, double units, double price) {
ItemNumber = item;
ProductName = product;
UnitsStock = units;
UnitPrice = price;
} //end constructor
//getter and setter methods for Television
public void setItemNumber (String item) { //setter for item number
this.ItemNumber = item;
} //end setter item number
public String getItemNumber() { //getter for item number
return ItemNumber;
} //end getter item number
public void setProductName (String product) { //setter for product name
this.ProductName = product;
} //end setter product name
public String getProductName() { //getter for product name
return ProductName;
} //end getter product name
public void setUnitsStock (double units) { //setter for units in stock
this.UnitsStock = units;
} //end setter units in stock
public double getUnitsStock() { //getter for units in stock
return UnitsStock;
} //end getter units in stock
public void setUnitPrice (double price) { //setter for unit price
this.UnitPrice = price;
} //end setter unit price
public double getUnitPrice() { //getter for unit price
return UnitPrice;
} //end getter for unit price
//calculate inventory value
public double getInventoryValue(){
return UnitsStock * UnitPrice;
}//end calculate inventory value
public void setCalculateInventory (double value){
this.CalculateInventory = value;
}
public double getCalculateInventory(){
double value = 0;
for(int i = 0; i < 5; i++){
value = getInventoryValue();
}
return value;
}
//end getter and setter methods for Television
} //end class Television
:主类:
import java.util.*;
public class InventoryPart2 {
public static void main (String args []){
//instantiate Television array
Television myTelevisions[] = new Television[5];
myTelevisions[0] = new Television ("0001", " Samsung UN46D6400",9,1599.99);
myTelevisions[1] = new Television ("0002", " Vizio XVT553SV",6,1299.00);
myTelevisions[2] = new Television ("0003", " Panasonic Viera TC-P50VT25",2,2079.99);
myTelevisions[3] = new Television ("0004", " Sony Bravia KDL-55EX720",8, 1889.99);
myTelevisions[4] = new Television ("0005", " LG Infinia 47LX9500",2,2099.00);
//output
for (int i = 0; i < myTelevisions.length; i++){
System.out.println("Product Number: \t\t" + myTelevisions[i].getItemNumber());
System.out.println("Product Name: \t\t\t" + myTelevisions[i].getProductName());
System.out.println("Number of Units in Stock: \t" + myTelevisions[i].getUnitsStock());
System.out.printf("Price per Unit: \t\t$%.2f\n", + myTelevisions[i].getUnitPrice());
System.out.printf("Value of Inventory: \t\t$%.2f\n", + myTelevisions[i].getInventoryValue());
System.out.println();
} // end output
for (int i = 0; i < 5; i++){ //output total inventory value
System.out.printf("Total Value of Inventory is: \t$%.2f\n", + myTelevision[i].getCalculateInventory());
System.out.println();
}//end output total inventory value
} //end method main
} //end class InventoryPart1
I get the following error code:
C:\Documents and Settings\AdminUser\My Documents\InventoryPart2.java:83: class, interface, or enum expected
import java.util.*;
^
1 errorTool completed with exit code 1
when compiling these source files:
public class Television { //class name and attributes
private String ItemNumber; //item # of product
private String ProductName; //product name
private double UnitsStock; //# of units in stock
private double UnitPrice; //Price per unit
private double InventoryValue; //The dollar value of the inventory in stock
private double CalculateInventory; //The total value of all of the inventory in stock
private double value;
//constructor
public Television (String item, String product, double units, double price) {
ItemNumber = item;
ProductName = product;
UnitsStock = units;
UnitPrice = price;
} //end constructor
//getter and setter methods for Television
public void setItemNumber (String item) { //setter for item number
this.ItemNumber = item;
} //end setter item number
public String getItemNumber() { //getter for item number
return ItemNumber;
} //end getter item number
public void setProductName (String product) { //setter for product name
this.ProductName = product;
} //end setter product name
public String getProductName() { //getter for product name
return ProductName;
} //end getter product name
public void setUnitsStock (double units) { //setter for units in stock
this.UnitsStock = units;
} //end setter units in stock
public double getUnitsStock() { //getter for units in stock
return UnitsStock;
} //end getter units in stock
public void setUnitPrice (double price) { //setter for unit price
this.UnitPrice = price;
} //end setter unit price
public double getUnitPrice() { //getter for unit price
return UnitPrice;
} //end getter for unit price
//calculate inventory value
public double getInventoryValue(){
return UnitsStock * UnitPrice;
}//end calculate inventory value
public void setCalculateInventory (double value){
this.CalculateInventory = value;
}
public double getCalculateInventory(){
double value = 0;
for(int i = 0; i < 5; i++){
value = getInventoryValue();
}
return value;
}
//end getter and setter methods for Television
} //end class Television
Main class:
import java.util.*;
public class InventoryPart2 {
public static void main (String args []){
//instantiate Television array
Television myTelevisions[] = new Television[5];
myTelevisions[0] = new Television ("0001", " Samsung UN46D6400",9,1599.99);
myTelevisions[1] = new Television ("0002", " Vizio XVT553SV",6,1299.00);
myTelevisions[2] = new Television ("0003", " Panasonic Viera TC-P50VT25",2,2079.99);
myTelevisions[3] = new Television ("0004", " Sony Bravia KDL-55EX720",8, 1889.99);
myTelevisions[4] = new Television ("0005", " LG Infinia 47LX9500",2,2099.00);
//output
for (int i = 0; i < myTelevisions.length; i++){
System.out.println("Product Number: \t\t" + myTelevisions[i].getItemNumber());
System.out.println("Product Name: \t\t\t" + myTelevisions[i].getProductName());
System.out.println("Number of Units in Stock: \t" + myTelevisions[i].getUnitsStock());
System.out.printf("Price per Unit: \t\t$%.2f\n", + myTelevisions[i].getUnitPrice());
System.out.printf("Value of Inventory: \t\t$%.2f\n", + myTelevisions[i].getInventoryValue());
System.out.println();
} // end output
for (int i = 0; i < 5; i++){ //output total inventory value
System.out.printf("Total Value of Inventory is: \t$%.2f\n", + myTelevision[i].getCalculateInventory());
System.out.println();
}//end output total inventory value
} //end method main
} //end class InventoryPart1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如 @Greg 和 @EboMike 所说,请确保将代码拆分为两个文件:Television.java 和 InventoryPart2.java。
那么,这里有一个拼写错误:
这是 myTelevision s[i]。
如果你解决了我告诉你的问题,你的代码就可以正常运行......不用担心。 :)
编辑:
我不应该在这里检查代码的逻辑,但如果最后一个周期是为了计算总库存总和,那么它应该是这样的:
As @Greg and @EboMike stated, make sure you split your code in two files: Television.java and InventoryPart2.java.
Then, you have a typo here:
It's myTelevision s[i].
If you fix what I'm telling you, your code runs fine... no worries. :)
EDIT:
I shouldn't be checking your code's logic here but if the last cycle is meant to compute the total inventory sum it should be something like this:
这都是同一个文件吗? import 语句需要位于顶部。
错误消息清楚地显示了哪一行导致了问题。
Is this all the same file? The import statement needs to go to the top.
The error message clearly shows you which line is causing a problem.
有一行
文件中间 。 Java 要求
import
声明位于文件的顶部,位于任何class
、interface
或之前>enum
声明。You've got a line
in the middle of your file. Java requires that
import
declarations go at the top of the file, before anyclass
,interface
, orenum
declarations.我突然想到两个问题:
正如错误消息所示,您的导入语句不合适。导入位于顶部,位于包声明之后和类声明之前。
假设您发布的代码实际上全部在一个文件中,您需要将其分成两个文件。一个 .java 文件中不能有两个公共类。
Two problems jump out at me:
As the error message says, you have an import statement out of place. Imports go at the top, after the package declaration and before the class declaration.
Assuming that the code you posted is in fact all in one file, you need to separate it into two files. You can't have two public classes in one .java file.