Java, Check if integer is multiple of a number

后端 未结 4 1055
走了就别回头了
走了就别回头了 2020-12-02 23:06

How do I check if a Java integer is a multiple of another number? For example, if int j is a multiple of 4.

4条回答
  •  佛祖请我去吃肉
    2020-12-02 23:42

    If I understand correctly, you can use the module operator for this. For example, in Java (and a lot of other languages), you could do:

    //j is a multiple of four if
    j % 4 == 0
    

    The module operator performs division and gives you the remainder.

提交回复
热议问题