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

后端 未结 2 619
臣服心动
臣服心动 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;
    
    0 讨论(0)
  • 2020-12-05 14:00
    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");
                 }
            }
    
        }
    }
    
    0 讨论(0)
提交回复
热议问题