为什么错误出现在第一个 return 和 (:) 运算符处?

why errors arise at the first return and (:) operator?

public int factorial(int number)
        {
            number <= 0 ? return 1 : return number * factorial(number - 1);
        }

编译器告诉我第一个 return 是一个无效的表达式,并且有一个 ;} 预计我应该做什么?

您错误地使用了三元运算符。你不能使用 return.

你真的想要这个:

return number <= 0 ? 1 : number * factorial(number - 1);

Docs:

condition ? consequent : alternative

The condition expression must evaluate to true or false. If condition evaluates to true, the consequent expression is evaluated, and its result becomes the result of the operation. If condition evaluates to false, the alternative expression is evaluated, and its result becomes the result of the operation. Only consequent or alternative is evaluated.

这意味着 consequentalternative 都应该计算为可以返回的值。