How to check if number is divisible by a certain number?

后端 未结 2 627
臣服心动
臣服心动 2020-12-05 13:19

I am using AndEngine to add sprites to the screen and come across using the movemodifier method.

I have two integers MaxDuration and MinDuration;

What i wan

2条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-05 13:49

    n % x == 0
    

    Means that n can be divided by x. So... for instance, in your case:

    boolean isDivisibleBy20 = number % 20 == 0;
    

    Also, if you want to check whether a number is even or odd (whether it is divisible by 2 or not), you can use a bitwise operator:

    boolean even = (number & 1) == 0;
    boolean odd  = (number & 1) != 0;
    

提交回复
热议问题