为什么我的程序没有正确编译?

时间:2012-11-19 08:13:40

标签: java compiler-errors

我编译代码时遇到错误。我相信我的逻辑很好,除了我在创建BushelBasket类时我必须犯了错误。我被告知不要以任何方式改变我的导师的主要方法。感谢您提前提供任何帮助!

public class AppleOrchard
{
    public static void main(String [] args)
    {
        System.out.println("Rick...");
        BushelBasket rick = new BushelBasket(0);
        rick.print();
        rick.pick(11);
        rick.pick(22);
        rick.print();
        rick.eat(4);
        rick.print();
        rick.spill();
        rick.print();

        System.out.println("Newt...");
        BushelBasket newt = new BushelBasket(100);
        newt.print();

        System.out.println( newt.isEmpty() );
        System.out.println( newt.isFull() );
        System.out.println( newt.getApples() );
        System.out.println( newt.roomLeftInBasket() );

        System.out.println("Michele...");
        BushelBasket michele = new BushelBasket(0);
        System.out.println( michele.isEmpty() );
        System.out.println( michele.isFull() );
        michele.pick(25);
        System.out.println( michele.isEmpty() );
        System.out.println( michele.isFull() );
        michele.pick(100);
        System.out.println( michele.isEmpty() );
        System.out.println( michele.isFull() );

        System.out.println("Herman...");
        BushelBasket herman = new BushelBasket(-5);  // should default to 0
        herman.print();

        System.out.println("Jon...");
        BushelBasket jon = new BushelBasket(300);  // should default to 125
        jon.print();

        System.out.println("Ron...");
        BushelBasket ron = new BushelBasket(20);  // starts with 20
        ron.print();
        ron.eat(50);  // can only eat down to zero apples
        ron.print();  // should see zero apples
        ron.eat(10);  // back to 10
        ron.pick(1000);  // basket can only hold 125 apples
        ron.print();  // should print 125

        System.out.println("Gary...");
        BushelBasket gary = new BushelBasket();  // should default to 0
        gary.print();
    }
}
class BushelBasket()
{
        public void spill()
        {
            apples = 0;
        }

        public void pick(int x)
        {
            apples = apples + x;
        }

        public void eat(int x)
        {
            apples = apples - x;
        }

        public int getApples()
        {
            return apples;  
        }

        public void print()
        {
            int x = getApples();
            System.out.println("This bushel basket has " + x + " apples in it.");
        }

        public boolean isEmpty()
        {
            int emtpy = 0;

            if (apples <= emtpy)
            {   
                return true;
            }

            else 
            {
                return false;
            }
        }

        public boolean isFull()
        {
            int full = 125;

            if (apples >= full)
            {   
                return true;
            }

            else 
            {
                return false;
            }

        }

        public boolean roomLeftInBasket()
        {
            int full = 125;

            if (apples < full)
            {   
                return true;
            }

            else 
            {
                return false;
            }

        }

}

3 个答案:

答案 0 :(得分:3)

显然,您忘了声明apples变量和构造函数:

class BushelBasket {
  int apples;

  BushelBasket() {
  }  

  BushelBasket(int apples) {
    this.apples = apples;
  }

  ...
}

您的编译器错误会有所帮助,顺便说一句。

答案 1 :(得分:1)

这一行(和类似的)......

 BushelBasket michele = new BushelBasket(0);

...正在尝试使用BushelBasket参数调用int上的构造函数。我没有看到任何这样的构造函数,也没有看到你一直提到的apples变量的声明。

这两点上的编译器错误应该非常清楚。仔细阅读编译器错误消息非常重要。

答案 2 :(得分:1)

您的Java应用无法编译。

您需要在apples

中声明变量BushelBasket

并且您缺少指定apples

的构造函数
public BushelBasket(int apples)
{
   this.apples = apples;

}

BTW,

而不是

class BushelBasket()

应该是

class BushelBasket