如果语句未正确运行

时间:2018-02-23 19:08:00

标签: java if-statement bluej

我正在尝试做的是让我的程序运行一个方法来制作柠檬水(makeLemonade),当且仅当我有足够的成分来制作柠檬水时,我仍然从为我提供的测试中得到错误告诉我即使我没有足够的成分,我的柠檬水也会被制作出来。

这是我现在为if语句给我带来麻烦的代码。我试过用&&和||以及> =和>的不同混合到目前为止无济于事。

 public int makeLemonade() {
    if (lemons >= 6 && gallonsOfWater >= 1 && cupsOfSugar >= 1 && emptyGlasses >= 8) {
        lemons = lemons - 6;
        gallonsOfWater = gallonsOfWater - 1;
        cupsOfSugar = cupsOfSugar - 1; 
        emptyGlasses = emptyGlasses - 8;
        glassesOfLemonade = glassesOfLemonade + 8;
        return glassesOfLemonade;
    } else {
        return 0;
    }
}

这是我的测试现在给我的错误

“方法测试makeLemonade因活动3而失败。

执行了以下代码:

LemonadeStand ls = new LemonadeStand(5, 2, 2, 16, 1.1);
ls.makeLemonade();

即使没有足够的柠檬来制作柠檬水,也修改了字段。“

到目前为止,这是完整的代码

    /**
 * LemonadeStand.java
 * 
 */

//Put any imports below this line.

/**
 * Short, one-line description of LemonadeStand class here.
 * 
 * Optionally, include a paragraph that provides a more 
 * detailed description.
 *
 * @author Nicholas Thomas 
 * @version 2/19/2018
 */
public class LemonadeStand
{
    //Put instance variables below this line.  
    private int lemons;
    private int gallonsOfWater;
    private int cupsOfSugar;
    private int emptyGlasses;
    private double price;
    private double income;
    private int glassesOfLemonade;
    /** No arg constructor.
     * LemonadeStand Constructor
     *
     */
    public LemonadeStand()
    {
        lemons = 0; 
        gallonsOfWater = 0;
        cupsOfSugar = 0;
        glassesOfLemonade = 0;
        emptyGlasses = 0;
        price = 0;
        income = 0;
    }

    /** Contructor.
     * LemonadeStand Constructor
     *
     * @param newLemons A parameter
     * @param newGallonsOfWater A parameter
     * @param newCupsOfSugar A parameter
     * @param newEmptyGlasses A parameter
     * @param newPrice A parameter
     */
    public LemonadeStand(int newLemons, int newGallonsOfWater, 
    int newCupsOfSugar, int newEmptyGlasses, double newPrice)
    {
        setLemons(newLemons); 
        setGallonsOfWater(newGallonsOfWater);
        setCupsOfSugar(newCupsOfSugar);
        setEmptyGlasses(newEmptyGlasses);
        setPrice(newPrice); 
        glassesOfLemonade = 0;
        income = 0;
    }

    /** Main method of the program.
     * Method main
     *
     * @param args A parameter
     */
    public static void main(String[] args)
    {
        LemonadeStand lemonadeStand = new LemonadeStand(15, 3, 4, 20, 1.5);
        lemonadeStand.makeLemonade();
        System.out.println(lemonadeStand.getLemons());
        System.out.println(lemonadeStand.getGallonsOfWater());
        System.out.println(lemonadeStand.getCupsOfSugar());
        System.out.println(lemonadeStand.getGlassesOfLemonade());
    }

    /** Mutator to change the amount of lemons.
     * Method setLemons
     *
     * @param newLemons A parameter
     * @return newLemons
     */
    public int setLemons(int newLemons)
    {
        if (lemons < 0)
        {
            lemons = newLemons;
            return newLemons;
        }
        else
        { 
            return 0;
        }

    }

    /** Mutator to change gallons of water.
     * Method setGallonsOfWater
     *
     * @param newGallonsOfWater A parameter
     * @return gallonsOfWater
     */
    public int setGallonsOfWater(int newGallonsOfWater)
    {
        if (gallonsOfWater < 0)
        {
            gallonsOfWater = newGallonsOfWater;
            return gallonsOfWater;
        }
        else
        {
            return 0;
        }
    }

    /** Mutator to set cups of sugar.
     * Method setCupsOfSugar
     *
     * @param newCupsOfSugar A parameter
     * @return cupsOfSugar
     */
    public int setCupsOfSugar(int newCupsOfSugar)
    {
        if (cupsOfSugar < 0)
        {
            cupsOfSugar = newCupsOfSugar;
            return cupsOfSugar;
        }
        else
        { 
            return 0;
        }
    }

    /** Mutator to modify the number of empty glasses.
     * Method setEmptyGlasses
     *
     * @param newEmptyGlasses A parameter
     * @return emptyGlasses
     */
    public int setEmptyGlasses(int newEmptyGlasses)
    {
        if (emptyGlasses < 0)
        {
            emptyGlasses = newEmptyGlasses;
            return emptyGlasses;
        }
        else
        {
            return 0;
        }
    }

    /** Mutator to modify the glasses of lemonade.
     * Method setGlassesOfLemonade
     *
     * @param newGlassesOfLemonade A parameter
     * @return glassesOfLemonade
     */
    public int setGlassesOfLemonade(int newGlassesOfLemonade)
    {
        if (glassesOfLemonade < 0)
        {
            glassesOfLemonade = newGlassesOfLemonade;
            return glassesOfLemonade;
        }
        else
        {
            return 0;
        }
    }

    /** Mutator to change the price.
     * Method setPrice
     *
     * @param newPrice A parameter
     * @return price
     */
    public double setPrice(double newPrice)
    {
        if (price < 0)
        {
            price = newPrice;
            return price;
        }
        else
        {
            return 0;
        }
    }

    /** Mutator to set the income.
     * Method setIncome
     *
     * @param newIncome A parameter
     * @return income
     */
    public double setIncome(double newIncome)
    {
        if (income < 0)
        {
            income = newIncome;
            return income;
        }
        else
        {
            return 0;
        }
    }

    /** Accessor to make lemonade.
     * Method makeLemonade
     *
     * @return The return value
     **/

    public int makeLemonade()
    {
        if (lemons >= 6 && gallonsOfWater >= 1 && cupsOfSugar >= 1 
        && emptyGlasses >= 8)
        {
            lemons -= 6;
            gallonsOfWater -= 1;
            cupsOfSugar -=  1; 
            emptyGlasses -= 8;
            glassesOfLemonade += 8;
            return glassesOfLemonade;
        }
        else 
        {
            return 0;
        }
    }

    /** Accessor to lemonade selling.
     * Method sellLemonade
     *
     * @return The return value
     */
    public int sellLemonade()
    {
        if (glassesOfLemonade <= 1)
        {
            makeLemonade();
            return 0;}
        else 
        {
            glassesOfLemonade = glassesOfLemonade - 1;
            income = income + price;
            return glassesOfLemonade; 
        }

    }

    /** Accessor to get number of lemons.
     * Method getLemons
     *
     * @return The return value
     */
    public int getLemons()
    {
        return lemons;
    }

    /** Accessor to return gallons of water.
     * Method getGallonsOfWater
     *
     * @return The return value
     */
    public int getGallonsOfWater()
    {
        return gallonsOfWater;
    }

    /** Accessor to return cups of sugar.
     * Method getCupsOfSugar
     *
     * @return The return value
     */
    public int getCupsOfSugar()
    {
        return cupsOfSugar;
    }

    /** Accessor to return the value of empty glasses.
     * Method getEmptyGlasses
     *
     * @return The return value
     */
    public int getEmptyGlasses()
    {
        return emptyGlasses;
    }

    /** Accessor to return glasses of lemonade.
     * Method getGlassesOfLemonade
     *
     * @return The return value
     */
    public int getGlassesOfLemonade()
    {
        return glassesOfLemonade;
    }

    /** Accessor to return the price.
     * Method getPrice
     *
     * @return The return value
     */
    public double getPrice()
    {
        return price;
    }

    /** Accesor to return the income rate.
     * Method getIncome
     *
     * @return The return value
     */
    public double getIncome()
    {
        return income;
    }

    /** Accessor for lemonade selling.
     * Method sellMoreLemonade
     *
     * @param requestedGlasses A parameter
     * @return The return value
     */
    public int sellMoreLemonade(int requestedGlasses)
    {
        return 0;
    }
}

0 个答案:

没有答案