2011-11-05 123 views

回答

59

的倍数使用remainder operator(也被称为modulo operator),它返回除法的余数,并检查它是否是零:

if (j % 4 == 0) { 
    // j is an exact multiple of 4 
} 
+0

检查j!= 0也是,如果j == 0它将导致0 –

+2

@AkhilDad,'0'仍然是'4'的倍数。 – paxdiablo

2

使用模

每当数字x是某个数字y的倍数时,则总是x%y等于0,这可用作检查。所以使用

if (j % 4 == 0) 
5

如果我理解正确,可以使用模块操作符。例如,在Java(和许多其他语言),你可以这样做:

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

模块操作员进行分工,让您的剩余部分。

+0

其“模”,而不是“模块” – 2017-05-11 15:18:49

-1
//More Efficiently 
public class Multiples { 
    public static void main(String[]args) { 

     int j = 5; 

     System.out.println(j % 4 == 0); 

    } 
}