2016-11-22 84 views
0

我正在研究一组代码,在每个“转向”(转弯时是所有3匹马都转过去)之后,相位上升了1.但是,当我运行它并输出相位最后总是会回到0.在这种情况下实施阶段计数的最佳方式是什么?在while语句中实现一个计数

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++; 
} 
+2

将'int phase = 0;'移到循环外部。您每次迭代都将其重置为0。 –

回答

3

变化

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

    .... 
} 

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

+1

菜鸟的错误。谢谢! – Steave