如何检查数字是否可以被某个数字整除? [英] How to check if number is divisible by a certain number?

查看:162
本文介绍了如何检查数字是否可以被某个数字整除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AndEngine将sprite添加到屏幕并使用movemodifier方法。

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

我有两个整数
MaxDuration和MinDuration;

I have two integers MaxDuration and MinDuration;

我想要做的是当用户获得某个增量的分数时。

What i want to do is when the user gets to a score of a certain increment.

例如。当用户达到40(整数变化)时用户达到20(整数变化)。所以基本上按20计算,每次得分满足一个可被20整除的数字变化。我希望这是有道理的。

Like for example.. when the user gets to 20(the integer changes) when the user gets to 40(the integer changes). So basically count by 20 and every time the score meets a number divisible by 20 the integer's change. I hope this makes sense.

有没有任何方法或方法可以做到这一点?我有一个UpdateTime处理程序,可以检查几乎每秒的分数。

Is there any method or way to do this? I have an UpdateTime handler that can check the score just about every second.

任何想法?

推荐答案

n % x == 0

表示n可以除以x。所以...例如,在你的情况下:

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

boolean isDivisibleBy20 = number % 20 == 0;

此外,如果你想检查数字是偶数还是奇数(是否可被2整除)或不),你可以使用按位运算符:

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;

这篇关于如何检查数字是否可以被某个数字整除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆