0

好的,分离我的代码。现在,当我运行时,我只收到一个错误,如下所示:

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: InventoryPro2.MobilePhone
    at InventoryPro2.InventoryPro2.main(InventoryPro2.java:12)
Java Result: 1

我从以前的回复中得到的是,我需要将 MobilePhone 类和 InventoryPro2 类移动到同一个项目中的单独的 .java 文件中。我错过了重点吗?正如我之前所说,我对此是全新的,在同时学习另一种编程语言的同时快速学习所有这些是很糟糕的。感谢您迄今为止的帮助,非常感谢。

   package InventoryPro2;



    public class InventoryPro2 {

        public static void main(String args[]) {
            MobilePhone MobilePhoneObject = new MobilePhone();
            MobilePhoneObject.MobilePhone();
            System.out.println("Mobile Phone Inventory");
            System.out.println();//skips a line

            MobilePhone[] Phones = new MobilePhone[5];

            Phones[0] = new MobilePhone(1, "Motorola", "Electronics", 98, 150.00);
            Phones[1] = new MobilePhone(2, "LG", "Electronics", 650, 199.99);
            Phones[2] = new MobilePhone(3, "Samsung", "Electronics", 125, 200.25);
            Phones[3] = new MobilePhone(4, "Nokia", "Electronics", 200, 100.05);
            Phones[4] = new MobilePhone(5, "IPhone", "Electronics", 138, 125.75);


            for (int count = 0; count < Phones.length-1; count++) {



                System.out.printf("Product Number:  %1f\n", Phones[count].getproductNumber());
                System.out.printf("Product Name:  %s\n", Phones[count].getname());
                System.out.printf("Units In Stock:  %.2f\n", Phones[count].getunitsInStock());
                System.out.printf("Unit Price: $%4.2f\n", Phones[count].getunitPrice());
                System.out.printf("Inventory Value:  $%4.2f\n", Phones[count].gettotalInv());
                System.out.println(); //blank line to seperate products

            }
        }
    }

package inventorypro2;

class MobilePhone { // Create class to store values

    private double productNumber; // Variables
    private String name;
    private String department;
    private double unitsInStock;
    private double unitPrice;

    public MobilePhone() {
        this(0.0, "", "", 0.0, 0.0);
    }

    public MobilePhone(double productNumber, String name, String department,
            double unitsInStock, double unitPrice) { //assign variables
        this.productNumber = productNumber;
        this.name = name;
        this.department = department;
        this.unitsInStock = unitsInStock;
        this.unitPrice = unitPrice;
    }

    public double getproductNumber() { // retrieve values
        return productNumber;
    }

    public String getname() {
        return name;
    }

    public String getdepartment() {
        return department;
    }

    public double getunitPrice() {
        return unitPrice;
    }

    public double getunitsInStock() {
        return unitsInStock;
    }

    public void setproductNumber(double productNumber) {
        this.productNumber = productNumber;
    }

    public void setname(String name) {
        this.name = name;
    }

    public void setdepartment(String department) {
        this.department = department;
    }

    public void setunitPrice(double unitPrice) {
        this.unitPrice = unitPrice;
    }

    public void setunitsInStock(double unitsInStock) {
        this.unitsInStock = unitsInStock;
    }

    public double gettotalInv() {
        return getunitPrice() * getunitsInStock();
    }
}
4

2 回答 2

3

Phones[0] = count + 1;

左边不是整数,但右边是整数。

关于您最近的编辑,我建议每个文件放置一个类(并将它们公开)。

或者,创建一个内部类。但是文件应该有一个外部类(不必是这样的:Java: Multiple class declarations in one file),但我认为它会使事情变得更简单。

于 2013-04-13T03:46:18.850 回答
0

电话[0] = 计数 + 1;

你想在这里做什么?您正在将整数(int类型)分配给 MobilePhone(MobilePhone类型)!删除那条线,你会很高兴。

于 2013-04-13T03:46:38.973 回答