【问题标题】:Having issues with code output with my program. Updated with new bushel basket class我的程序出现代码输出问题。更新了新的蒲式耳篮类
【发布时间】:2012-11-19 08:51:15
【问题描述】:

(现在有更新的代码)在尝试从我的程序中获取正确的输出时遇到严重问题。我需要从这个程序得到一个特定的输出,它应该是这个输出:

Rick...
This bushel basket has 0 apples in it.
This bushel basket has 33 apples in it.
This bushel basket has 29 apples in it.
This bushel basket has 0 apples in it.
Newt...
This bushel basket has 100 apples in it.
false
false
100
25
Michele...
true
false
false
false
false
true
Herman...
This bushel basket has 0 apples in it.
Jon...
This bushel basket has 125 apples in it.
Ron...
This bushel basket has 20 apples in it.
This bushel basket has 0 apples in it.
This bushel basket has 125 apples in it.
Gary...
This bushel basket has 0 apples in it.

但是我收到了输出:

Rick...
This bushel basket has 0 apples in it.
This bushel basket has 33 apples in it.
This bushel basket has 29 apples in it.
This bushel basket has 0 apples in it.
Newt...
This bushel basket has 100 apples in it.
false
false
100(THIS IS WHERE THE BAD OUTPUT STARTS)
true
Michele...
true
false
false
false
false
true
Herman...
This bushel basket has -5 apples in it.
Jon...
This bushel basket has 300 apples in it.
Ron...
This bushel basket has 20 apples in it.
This bushel basket has -30 apples in it.
This bushel basket has 960 apples in it.
Gary...
This bushel basket has 0 apples in it.

这里是代码

    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(0); // should default to 0
            gary.print();
        }
    }



    class BushelBasket
{
        int apples;

        BushelBasket(int apples) 
        {
    if (apples > 0 && apples < 125)
      this.apples = apples;
        }

        public void spill()
        {
            apples = 0;
        }

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

        public void eat(int x)
        {
            if (!isEmpty())
                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;
            }

        }

}

【问题讨论】:

  • 感谢 Bhavik Shah 我没有意识到没有正确发布!

标签: java output


【解决方案1】:

改变构造函数

BushelBasket(int apples) 
{
   if (apples < 0 )
      this.apples = 0;
   else if(apples >125)
      this.apples = 125;
   else
      this.apples=apples;
}

【讨论】:

    【解决方案2】:

    纽特

    你的代码有

    System.out.println( newt.getApples() );                      // integer
    System.out.println( newt.roomLeftInBasket() );               // boolean
    

    这不会像你期望的那样返回100 25,它会返回100 true

    您似乎需要更改 roomLeftInBasket() 方法以返回篮子中剩余的金额,或更改代码使其看起来像:

    if (newt.roomLeftInBasket())
    {
      System.out.println( newt.fullBasket() - newt.getApples());
    }
    

    但您必须创建 fullBasket() 方法

    赫尔曼

    在 Herman 的构造函数中,你给他的篮子 -5 个苹果。如果你不能有一个否定的篮子,你将不得不改变构造函数来检查这个。

    乔恩

    与 Herman 相同,但您需要检查创建的篮子的价值是否大于一个完整的篮子并相应地进行更改。

    罗恩

    对于 Ron,您需要检查给 eat() 的值是否大于篮子中的值,并处理 Ron 尝试吃的苹果多于篮子中的苹果的情况。

    【讨论】:

    • @BhavikShah 你这样声明:public boolean roomLeftInBasket() {
    • 试过了,还是不行。尝试编译时出错。创建公共布尔 roomLeftInbasket() 但没有帮助
    • 您已经拥有public boolean roomLeftInbasket()。你需要public int fullBasket();
    • 很抱歉来晚了,我不知道我还需要做什么来修复我的程序。公共 intfullbasket() 应该包含什么?
    • 一篮子苹果的数量。将 public boolean roomLeftInbasket() 更改为返回 int 而不是 boolean 可能更有意义。无论哪种方式,您都需要获得一个数字(而不是真/假)。
    猜你喜欢
    • 2017-08-28
    • 1970-01-01
    • 2021-02-09
    • 2019-07-18
    • 1970-01-01
    • 1970-01-01
    • 2016-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多