0
在一个类中,我有一个方法在这里应该为每个调用添加int mWeight
,然后每调用一次,就会移动mSize
数组。在我调用getter的主要方法中,我没有在数组中获得更高的字符串值。使用整数通过字符串数组为每个方法调用增加
这里是相关的类变量和getter。
// Amount of weight to gain after eating
final float WEIGHT_GAIN = 0.25f;
// Its weight in pounds
float mWeight;
// Size, either "tiny", "small", "average", or "large"
String mSize[] = {"tiny", "small", "average", "large"};
int dogSize;
String getSize(){
return mSize[dogSize];
该方法和它需要做的事情。
/*
* feed
*
* Feeds the Dog.
*
* Side-effect: 1. The Dog gains weight, specifically WEIGHT_GAIN
* 2. Every 3 meals, the Dog grows to a larger size, if
* possible
* i.e. "tiny" (3 meals later ->) "small" (3 meals later ->)
* "average" (3 meals later ->) "large"
* the Dog cannot exceed the "large" size or shrink smaller than
* "tiny"
* @return nothing
*/
void feed(){
while(mWeight < mSize.length)
mWeight ++;
mWeight = mWeight * WEIGHT_GAIN;
dogSize += (int)mWeight/3;
if (dogSize > mSize.length)
dogSize = mSize.length;
}
难道你没有忘记'while'后的花括号吗? 'dogSize'在哪里申报? – alfasin 2015-02-11 07:50:05
当我看到“每三次调用一次”时,我正在考虑实例范围计数器和模运算符。 – 2015-02-11 08:04:53
编辑制成。 'dogGender'应该是'dogSize',在成员变量中声明。 – TonyKazanjian 2015-02-11 17:39:37