如果递归调用应该停止并且当 a 或 b 变为 0 时 return 为什么这个最大公约数程序的输出为 -1

Why the output of this greatest common divisor program is coming as -1 if the recursive call should stop and return when a or b becomes 0

1.The 方法采用两个参数来计算最大公约数。 2.Instead返回a或b的值,程序returns-1.

public static int gcd(int a,int b)
    {

        if(a==0 && b>0)
        {
            // returns when a becomes  0
            return b;
        }
        else if(b==0 && a>0)
        {
            //returns when b becomes 0
            return a;

        }
        else if(a>b)
         gcd(b,a%b);
        else 
            gcd(a,b%a);

        return -1;
    }

您也需要 return 递归调用。所以不需要return-1

public static int gcd(int a, int b) {

    if (a == 0 && b > 0) {
        // returns when a becomes  0
        return b;
    } else if (b == 0 && a > 0) {
        //returns when b becomes 0
        return a;

    } else if (a > b) {
        return gcd(b, a % b);
    } else {
        return gcd(a, b % a);
    }
}