我尝试使用 6%2,但它总是将值设为 2 而不是 0。为什么以及如何解决这个问题?
3 回答
3
如果(!(y%x)) { ... }
在您的情况下!(6%2)
将返回true。
(答案与问题中的原始答案非常相似)
于 2010-10-22T10:37:43.377 回答
1
我假设您想知道 Y=kX 对于给定 X 是否具有 k 整数值,因此 Y=5, X=3 失败(k 为 5/3),但 Y=6, X=2 通过(k 正好是 3)。您很高兴 k 是正数或负数。
这样,使用 Y 余数 X == 0 是一个很好的测试。顺便说一句,请注意负余数(例如 Y % 2 == 1 作为对负数的奇数测试失败,请使用 Y % 2 != 0 确定)
Java中的代码示例
public class Example {
public static void main(String[] args) {
System.out.println(isIntegerFactor(5,3)); // k is not an integer
System.out.println(isIntegerFactor(6,3)); // k is 2
System.out.println(isIntegerFactor(-6,-3)); // k is 2
System.out.println(isIntegerFactor(-6,3)); // k is -2
System.out.println(isIntegerFactor(6,-3)); // k is -2
}
public static boolean isIntegerFactor(int y, int x) {
return (y % x) == 0;
}
}
于 2010-10-22T11:59:34.560 回答
0
bool prime = PrimeTool.IsPrime(input_Number);
if (!prime)
{
Console.Write("multiple of other number");
Console.WriteLine(i);
}
于 2010-10-22T10:24:58.437 回答