Java 我返回负1的方法是否错误,它不会输出负1
我的GCD代码对于我输入的任何数字都是正确的,它返回一个输出。然而,我试图得到如果输入一个负数,那么输出一个负数1,但是由于某种原因失败了。负输入返回什么?它似乎对我有用。不过,我不确定为什么要使用d。您可以使用return-1;你忘了重新编译吗?它对我也有用。你忘了重新编译吗?Java 我返回负1的方法是否错误,它不会输出负1,java,Java,我的GCD代码对于我输入的任何数字都是正确的,它返回一个输出。然而,我试图得到如果输入一个负数,那么输出一个负数1,但是由于某种原因失败了。负输入返回什么?它似乎对我有用。不过,我不确定为什么要使用d。您可以使用return-1;你忘了重新编译吗?它对我也有用。你忘了重新编译吗? public class MathMethods { public static int GCD (int a, int b){ int c; int d; d = -1; if (a
public class MathMethods {
public static int GCD (int a, int b){
int c;
int d;
d = -1;
if (a < 0 || b < 0){
return d;
}else{
while (b!=0){
c=a%b;
a=b;
b=c;
}
return a;
}
}
public static void main (String[] args){
System.out.println("GCD of 15 and 5: " + MathMethods.GCD(15,5));
System.out.println("GCD of 12 and 18: " + MathMethods.GCD(12,18));
System.out.println("GCD of 42 and 56: " + MathMethods.GCD(42,56));
System.out.println("GCD of 126 and 84: " + MathMethods.GCD(126,84));
System.out.println("GCD of 72390 and 15000: " + MathMethods.GCD(72390,15000));
}
}