请问一下,这段java计算最大公因数输出不正确,请问问题出在哪里?谢谢
public class xmath {
public static double smaller(double x, double y) {
if (x < y) {
return x;
} else {
return y;
}
}
public static int maximum_common_factor(int a, int b) {
double smaller = smaller(a, b);
int maximum_common_factor = 1;
for (int i = 1; i < smaller + 1; i++) {
if (a % i == 0 & b % i == 0) {
maximum_common_factor = i;
}
}
return maximum_common_factor;
}
}
import jpype
jpyJVM = jpype.startJVM(jpype.getDefaultJVMPath())
_xmath = jpype.JClass('xmath')
def maximum_common_factor(a: int, b: int):
return _xmath.maximum_common_factor(a, b)
if __name__ == '__main__':
print(maximum_common_factor(2,4))
>>> 输出: 1,应该是2