使用InventoryPart 2程序的Java代码,不知道我做错了什么?

时间:2021-02-28 16:48:24

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

C:\ Documents and Settings \ AdminUser \ My Documents \ InventoryPart2.java:83:class,interface或enum expected import java.util。*; ^ 1错误

Tool completed with exit code 1

工具已完成,退出代码为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

4 个解决方案

#1


0  

As @Greg and @EboMike stated, make sure you split your code in two files: Television.java and InventoryPart2.java.

正如@Greg和@EboMike所述,请确保将代码拆分为两个文件:Television.java和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].

这是我的电视剧[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);

#2


0  

Is this all the same file? The import statement needs to go to the top.

这是同一个文件吗? import语句需要转到顶部。

The error message clearly shows you which line is causing a problem.

错误消息清楚地显示哪条线导致问题。

#3


0  

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.

在您的文件中间。 Java要求在任何类,接口或枚举声明之前,导入声明位于文件的顶部。

#4


0  

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.

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

  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.

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

#1


0  

As @Greg and @EboMike stated, make sure you split your code in two files: Television.java and InventoryPart2.java.

正如@Greg和@EboMike所述,请确保将代码拆分为两个文件:Television.java和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].

这是我的电视剧[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);

#2


0  

Is this all the same file? The import statement needs to go to the top.

这是同一个文件吗? import语句需要转到顶部。

The error message clearly shows you which line is causing a problem.

错误消息清楚地显示哪条线导致问题。

#3


0  

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.

在您的文件中间。 Java要求在任何类,接口或枚举声明之前,导入声明位于文件的顶部。

#4


0  

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.

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

  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.

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