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

2013/1/29

Greenfoot modulo

moobe moobe

2013/1/29

#
Hi, I'd like my code to be executed when i mod 10 equals 0. So why does this code not work?
if ( i mod 10 == 0)
        {
        //doSomething();
        }
danpost danpost

2013/1/29

#
That code does not work because the compiler has no idea what "mod" is. The correct mathematical operator that you are looking for is "%". Please refer to the Java tutorial page on operators. If your code has i incrementing each act cycle and the action to perform is to be executed each and every time that i is a multiple of 10, then you might want to use this:
i = (i + 1) % 10;
if (i == 0)
{
    // do something
}
That is, of course, unless you need to know how many cycles through ten have been executed.
moobe moobe

2013/1/29

#
Oh, I didn't know that, I'm sorry. But it works fine now, thank you!
You need to login to post a reply.