我已经突出显示了下面给我这个错误的代码。我想要做的是,如果 的值nserved
小于或等于 4,则返回该值,但是如果大于 4,则默认值为 0。
PS 我刚开始编程的时间不长,我试图让我的代码尽可能简单。谢谢您的帮助。
public class FoodItem
{
private String Name;
private String Description;
private int NumberServed;
private double Cost;
public FoodItem(String fname, String fdescription, int nserved, double fcost)
{
Name = fname;
Description = fdescription;
NumberServed = nserved;
Cost = fcost;
}
public string getFoodName()
{
return Name;
}
public void setFoodName(string fname)
{
Name = fname;
}
public string getFoodDescription()
{
return Description;
}
public void setFoodDescription(string fdescription)
{
Description = fdescription;
}
public int getNumberPeople()
{
return NumberServed;
}
public void setNumberPeople(int nserved)
{
NumberServed = nserved;
// Checks to see if the maximum number of people has been exceeded.
if (nserved <= 4)
{
return nserved;
}
else
{
return 0;
}
}
public double getFoodCost()
{
return Cost;
}
public void setFoodCost(double fcost)
{
Cost = fcost;
}
}