1

コードをコンパイルするときにエラーが発生し続けます。私のロジックは問題ないと思いますが、BuchelBasket クラスの作成中にエラーが発生したに違いありません。インストラクターから、メインメソッドを決して変更しないように言われました。事前に助けてくれてありがとう!

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

        }

}
4

3 に答える 3

3

applesどうやら、変数とコンストラクターも宣言するのを忘れていました。

class BushelBasket {
  int apples;

  BushelBasket() {
  }  

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

  ...
}

あなたのコンパイラエラーは役に立ちます。

于 2012-11-19T08:16:33.560 に答える
1

この行(および同様の行)...

 BushelBasket michele = new BushelBasket(0);

... パラメーターを使用してコンストラクターを呼び出そうとしBushelBasketていintます。applesそのようなコンストラクターも、参照し続ける変数の宣言もありません。

コンパイラ エラーは、これらの両方の点でかなり明確になっているはずです。コンパイラのエラー メッセージを注意深く読むことは非常に重要です。

于 2012-11-19T08:17:06.673 に答える
1

Java アプリをコンパイルできません。

applesで変数を宣言する必要がありますBushelBasket

そして、代入するコンストラクターがありませんapples

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

}

ところで、

それ以外の

class BushelBasket()

そのはず

class BushelBasket
于 2012-11-19T08:18:20.657 に答える