What does the following line do? Could someone help me write this line in "normal" code?
int change = (Math.random() - 0.5 < 0 ? -5 : 5);
What does the following line do? Could someone help me write this line in "normal" code?
int change = (Math.random() - 0.5 < 0 ? -5 : 5);
This is a ternary operator the way it works is :
condition ? (things to do if true) : (things to do if false);
In your code what it does is :
if value of Math.random() - 0.5 < 0
then assign change a values of -5
else
assign change a value of 5.
This line takes a random number (between 0 and 1) and subtracts 0.5. If that value is less than 0 then change is set to -5, otherwise 5.
int change;
if((Math.random() - 0.5) < 0)
{
change=-5;
}
else
{
change=5;
}