在while语句中实现计数

时间:2016-11-22 01:57:34

标签: java count

我正在研究一组代码,在每次“转弯”之后(转弯是所有3匹马都去的时候),阶段上升1.然而,当我运行它并输出阶段结束它总是回来在这种情况下实现阶段计数的最佳方法是什么?

while (horse1.getLocation() <= 250 && horse2.getLocation() <= 250 && horse3.getLocation() <= 250){
     int phase = 0; 
     horse1.move(phase);
     horse2.move(phase);    
     horse3.move(phase);

     horse1.location++;
     horse2.location++;
     horse3.location++;

     phase++;
}

1 个答案:

答案 0 :(得分:3)

更改为

int phase = 0; 
while (horse1.getLocation() <= 250 && horse2.getLocation() <= 250 &&
        horse3.getLocation() <= 250){

    ....
}

然后你可以在循环后使用它。如果在循环内,范围被限制在循环内部