Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 对于每个方法调用,使用int通过字符串数组递增_Java_Arrays_Methods - Fatal编程技术网

Java 对于每个方法调用,使用int通过字符串数组递增

Java 对于每个方法调用,使用int通过字符串数组递增,java,arrays,methods,Java,Arrays,Methods,在一个类中,我这里有一个方法,它应该为每次调用添加intmWeight,然后每调用三次,就在mSize数组中移动一次。在调用getter的main方法中,数组中没有任何更高的字符串值 下面是相关的类变量和getter // Amount of weight to gain after eating final float WEIGHT_GAIN = 0.25f; // Its weight in pounds float mWeight; // Size, either "

在一个类中,我这里有一个方法,它应该为每次调用添加int
mWeight
,然后每调用三次,就在
mSize
数组中移动一次。在调用getter的main方法中,数组中没有任何更高的字符串值

下面是相关的类变量和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;
}
/*
*喂
*
*喂狗。
*
*副作用:1。狗体重增加,特别是体重增加
*              2. 每三餐,狗就会长得更大,如果有的话
*可能的
*即“小”(三餐后->)“小”(三餐后->)
*“一般”(3餐后->)“大型”
*狗不能超过“大”尺寸或收缩小于
*“微小的”
*@一无所获
*/ 
无效馈送(){
while(mWeightmSize.length)
dogSize=mSize.length;
}

您可以为狗的进餐次数维护一个变量

final float WEIGHT_GAIN = 0.25f;
String mSize[] = {"tiny", "small", "average", "large"};
class Dog{
        int num_meals, mWeight, dogSize;
        public Dog(int weight){
            num_meals = 0;
            dogSize = 0;
            mWeight = weight;
        }
        public String getSize(){
            return mSize[dogSize];
        }
        void feed(){
            mWeight += mWeight * WEIGHT_GAIN;
            num_meals++;
            dogSize = num_meals/3 < mSize.length-1 ? num_meals/3 : mSize.length - 1; 
        }
}
最终浮子重量_增益=0.25f;
字符串mSize[]={“微小”、“小”、“平均”、“大”};
班犬{
int num_餐食,重量,狗尺寸;
公犬(体重){
num_=0;
狗的大小=0;
mWeight=重量;
}
公共字符串getSize(){
返回mSize[dogSize];
}
无效馈送(){
mWeight+=mWeight*体重增加;
num_++;
dogSize=num_fines/3
你忘了在
之后加上大括号了吗?哪里声明了
dogSize
?当我看到“每调用三次它”时,我认为是实例范围计数器和模运算符。编辑已完成
dogGender
应该在成员变量中声明
dogSize