increment a number in java until it gets to 100 than decrement down to 0 continously -


i'm making game there goalie. want him move , forth forever. have int called goalieposx (goalie position on x axis) , want go 1 until hits 200, go down 1 till 0 , repeat. i've tried folllowing

//this bit isnt in method, outside global varibale boolean forward=true  //this bit in method continiouly called nonstop if (goalieposx<200){         forward=true;     }     else if (goalieposx>200){         forward=false;     }     system.out.println(forward);      if(forward=true){         goalieposx++;         system.out.println("forward");     }     else if (forward=false){         goalieposx--;         system.out.println("backwards");     }  } 

this method called continously. prints true until gets 200, prints false. however, prints forward, never backward. conclusion is: boolean changes expected first if called, seems ignore condition

ive tried this

           if(forward = true){         if(goalieposx==200){              forward=false;          }         else{         goalieposx++;}     }     else{         if(goalieposx==0){             forward=true;         }         else{         goalieposx--;}              system.out.println(goalieposx);     } 

but doesnt work either, prints 1 2 etc upto 200 prints 200 forever. know how can solve this? if statement wrong idea altogether?

this why should never comparison boolean types in if, while, for, whatever. have done assignment in if statement:

if(forward=true)   

the above if statement evaluate true. problem is, compiles in java, syntax wise it's alright. compiler checks type of expression in if evaluates boolean or not. , does, it's ok it.

you need comparison:

if(forward==true) 

.. said, should not comparison boolean types. so, doing this:

if(forward) 

would enough.


you don't need else if in both conditions. else work fine. well, don't understand use of boolean variable @ all. seems don't need it. can change code to:

if (goalieposx<200){     // forward=true;     goalieposx++;     system.out.println("forward"); } else {     // forward=false;      goalieposx--;     system.out.println("backwards"); } 

what doing is, setting boolean variable, based on condition, , using boolean variable condition execute if-else block. well, whatever executing in 2nd if-else block, can moved in original if-else block, without taking of middle-actor boolean variable.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -