This site requires JavaScript, please enable it in your browser!
Greenfoot back
sosimple
sosimple wrote ...

2016/1/6

What does this mean in psuedo code?

sosimple sosimple

2016/1/6

#
(atWorldEdge() || atShip()) ? 20+Greenfoot.getRandomNumber(7) : 33;
danpost danpost

2016/1/6

#
What you gave in code only represents an int value. It is not a line of code in itself. It would be like these lines:
33;
// or (example in an Actor subclass)
getX();
// or
20+Greenfoot.getRandomNumber(7);
To have it make more sense, let us assign the value to a variable:
int value = (atWorldEdge() || atShip()) ? 20+Greenfoot.getRandomNumber(7) : 33;
Now, for what it means. In code, it is equivalent to the following:
int value;
if (atWorldEdge() || atShip())
{
    value = 20+Greenfoot.getRandomNumber(7);
}
else
{
    value = 33;
}
In other words, the condition before the question mark ( '?' ) determines which value to the right of the question mark to use. If the condition is true, use the first value (to the left of the colon ( ':' ) ); if false, use the other one (to the right of the colon).
You need to login to post a reply.