Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何检查Java中的可除性?_Java_Division - Fatal编程技术网

如何检查Java中的可除性?

如何检查Java中的可除性?,java,division,Java,Division,我正在尝试检查on number是否可被另一个数整除,目前我使用此方法: int x = 70; int y = 30; if(x/y == Math.round(x/y)) { ... } 有更简单的方法吗?你可以在你的条件下使用这样的模运算符 if (x%y == 0) 在你的条件下,你可以使用这样的模算子 if (x%y == 0) 一个好方法是使用模运算符,它在除以一个数字后返回余数,例如 5 % 2 = 1 (1 is the remainder after 5 is

我正在尝试检查on number是否可被另一个数整除,目前我使用此方法:

int x = 70;
int y = 30;

if(x/y == Math.round(x/y)) {
    ...
}

有更简单的方法吗?

你可以在你的条件下使用这样的模运算符

if (x%y == 0)

在你的条件下,你可以使用这样的模算子

if (x%y == 0)

一个好方法是使用模运算符,它在除以一个数字后返回余数,例如

5 % 2 = 1 (1 is the remainder after 5 is divided by 2)
因此,对于一个可以被另一个整除的数字,它应该有0的余数,即x%y=0

如果x%y==0 { //x可被y整除 } 其他的 { //x不能被y整除
} 一个好方法是使用模运算符,它在除以一个数字后返回余数,例如

5 % 2 = 1 (1 is the remainder after 5 is divided by 2)
因此,对于一个可以被另一个整除的数字,它应该有0的余数,即x%y=0

如果x%y==0 { //x可被y整除 } 其他的 { //x不能被y整除
}如果x%y==0对y==0执行附加检查,如果条件为真,则不要尝试除法…忽略“simpler”一秒钟,您的方法甚至不起作用,因为整数除法在Java中是如何工作的如果x%y==0对y==0执行附加检查,如果条件为真,则不要尝试除法…忽略“simpler”一秒钟,由于整型除法在Java中的工作方式,您的方法甚至不起作用