49

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 want to do is when the user gets to a score of a certain increment.

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.

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

Any ideas?

2 Answers 2

124
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; 
Sign up to request clarification or add additional context in comments.

7 Comments

To add to this, this symbole (%) is known as the modulus operator, or mod for short. So this function can be read as n mod x equals 0.
So in this case if the score is 20 and it checks if it is divisible by 20 the answer will be 1. so it would be false? correct? so why is it ==0?
so if the remainder is 0. then it is true?
mod is the remainder after the devision. So 20/20 = 1 and 20%20 = 0 (no remainder). 21%20 = 1
@coder_For_Life22 - You should note that 0 % 20 is also equal to 0, in case it makes a difference to your app.
|
6
package lecture3; import java.util.Scanner; public class divisibleBy2and5 { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("Enter an integer number:"); Scanner input = new Scanner(System.in); int x; x = input.nextInt(); if (x % 2==0){ System.out.println("The integer number you entered is divisible by 2"); } else{ System.out.println("The integer number you entered is not divisible by 2"); if(x % 5==0){ System.out.println("The integer number you entered is divisible by 5"); } else{ System.out.println("The interger number you entered is not divisible by 5"); } } } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.