在 InventoryPart 2 程序上工作的 Java 代码,不知道我做错了什么?

发布于 2024-11-02 01:24:56 字数 4154 浏览 2 评论 0原文

我收到以下错误代码:

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 error

Tool 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

木落 2024-11-09 01:24:56

正如 @Greg 和 @EboMike 所说,请确保将代码拆分为两个文件:Television.javaInventoryPart2.java

那么,这里有一个拼写错误:

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();
}

这是 myTelevision s[i]。

如果你解决了我告诉你的问题,你的代码就可以正常运行......不用担心。 :)

编辑:

我不应该在这里检查代码的逻辑,但如果最后一个周期是为了计算总库存总和,那么它应该是这样的:

    //output total inventory value
    double total = 0.0;
    for (int i = 0; i < 5; i++){ 
        total +=  myTelevisions[i].getCalculateInventory();            
    }
    System.out.printf("Total Value of Inventory is: \t$%.2f\n", total);

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:

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();
}

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:

    //output total inventory value
    double total = 0.0;
    for (int i = 0; i < 5; i++){ 
        total +=  myTelevisions[i].getCalculateInventory();            
    }
    System.out.printf("Total Value of Inventory is: \t$%.2f\n", total);
热风软妹 2024-11-09 01:24:56

这都是同一个文件吗? 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.

趁年轻赶紧闹 2024-11-09 01:24:56

有一行

import java.util.*;

文件中间 。 Java 要求 import 声明位于文件的顶部,位于任何 classinterface之前>enum 声明。

You've got a line

import java.util.*;

in the middle of your file. Java requires that import declarations go at the top of the file, before any class, interface, or enum declarations.

ゝ杯具 2024-11-09 01:24:56

我突然想到两个问题:

  1. 正如错误消息所示,您的导入语句不合适。导入位于顶部,位于包声明之后和类声明之前。

  2. 假设您发布的代码实际上全部在一个文件中,您需要将其分成两个文件。一个 .java 文件中不能有两个公共类。

Two problems jump out at me:

  1. 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.

  2. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文