Steave
Steave

Reputation: 29

Implementing a count in a while statement

I am working on a set of code that after each "turn" (a turn is when all 3 horses go), phase goes up by 1. However, when I run it and output phase at the end it always comes back as 0. What is the best way to implement a count of phase in this situation?

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

Upvotes: 0

Views: 49

Answers (1)

Scary Wombat
Scary Wombat

Reputation: 44854

change to

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

    ....
}

then you can use it after the loop. If inside the loop the scope is restricted to inside the loop

Upvotes: 3

Related Questions