如果语句不 运行 正确
If statement not running correctly
我想要做的是让我的程序 运行 一种制作柠檬水的方法 (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;
}
}
这是我的测试现在给我的错误
“活动 3 的 makeLemonade 方法测试失败。
执行了以下代码:
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;
}
}
代码的主要问题是 setter-usage。例如:
if (gallonsOfWater < 0) {
gallonsOfWater = newGallonsOfWater;
return gallonsOfWater;
} else {
return 0;
}
LemonadeStand
的 gallonsOfWater
字段已初始化,例如在第二个构造函数中调用 setGallonsOfWater(newGallonsOfWater);
。
让我们假设您将值 3 传递给此构造函数,因此它将是 setGallonsOfWater(3);
。在 setter 中,这导致 3 < 0
,因此
- return 值为 0 为 setter。
- 未设置字段
gallonsOfWater
的值。
(我假设调试在这里也会有所帮助)
我想要做的是让我的程序 运行 一种制作柠檬水的方法 (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;
}
}
这是我的测试现在给我的错误
“活动 3 的 makeLemonade 方法测试失败。
执行了以下代码:
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;
}
}
代码的主要问题是 setter-usage。例如:
if (gallonsOfWater < 0) {
gallonsOfWater = newGallonsOfWater;
return gallonsOfWater;
} else {
return 0;
}
LemonadeStand
的 gallonsOfWater
字段已初始化,例如在第二个构造函数中调用 setGallonsOfWater(newGallonsOfWater);
。
让我们假设您将值 3 传递给此构造函数,因此它将是 setGallonsOfWater(3);
。在 setter 中,这导致 3 < 0
,因此
- return 值为 0 为 setter。
- 未设置字段
gallonsOfWater
的值。
(我假设调试在这里也会有所帮助)