不兼容的类型错误我无法弄清楚

时间:2013-04-13 03:44:04

标签: java

好的,我的代码分开了。现在,当我跑步时,我得到的错误如下:

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

2 个答案:

答案 0 :(得分:3)

Phones[0] = count + 1;

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

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

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

答案 1 :(得分:0)

  

电话[0] =计数+ 1;

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