我想知道是否可以用最少的代码来实现此目的:
for (int x = 1; x < 10; x++){
/*I want to replace this condition with (x%number == 0)
instead of writing out condition for every number, but
it did not work with for (int number = 1; number <= 3; number++)
in (x%number == 0), as it prints out every x and number
*/
if ((x%1) == 0 && (x%2) == 0 & (x%3) == 0){
System.out.println(success!);
}
}
最佳答案
我认为
x % a == 0 && x % b == 0 && x % c == 0
等于
x % (a * b * c) == 0
更新
乘法不正确,需要使用LCM : x % lcm(a, b, c)
关于Java for循环的几个条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12018597/