Scala阶乘语法错误
我编写了以下代码:Scala阶乘语法错误,scala,factorial,Scala,Factorial,我编写了以下代码: def factorial(x: Int, factorial( x => { if (x == 0) 1 else x * factorial(x - 1) })): Int = factorial(3) 但我得到了一个错误: <console>:1: error: ':' expected but '(' found. :1:错误:':'应为,但找到了'('。 您的def的参数项是阶乘,这是不正确的。请尝试: def factorial(x:Int)
def factorial(x: Int, factorial( x => { if (x == 0) 1 else x * factorial(x - 1) })): Int = factorial(3)
但我得到了一个错误:
<console>:1: error: ':' expected but '(' found.
:1:错误:':'应为,但找到了'('。
您的def的参数项是阶乘,这是不正确的。请尝试:
def factorial(x:Int): Int =
if (x == 0) 1
else x * factorial(x - 1)
您的def参数项中有factorial
,这是不正确的。请尝试:
def factorial(x:Int): Int =
if (x == 0) 1
else x * factorial(x - 1)
这部分代码有意义(至少在语法上):
…
中出现的是函数的参数。您在x:Int
def factorial(x: Int, ...): Int = factorial(3)
但是它在语法上有点偏离了正轨,factorial(x=>{if(x==0)1 else x*factorial(x-1)}
。我真的不知道该怎么说,除了它肯定不是一个函数参数。这是一个表达式,你应该把它放在函数体中(在=
之后),不在参数列表中。这部分代码有意义(至少在语法上):
…
中出现的是函数的参数。您在x:Int
def factorial(x: Int, ...): Int = factorial(3)
但是它在语法上有点偏离了正轨,factorial(x=>{if(x==0)1 else x*factorial(x-1)}
。我真的不知道该怎么说,除了它肯定不是一个函数参数。这是一个表达式,你应该把它放在函数体中(在=
之后),不在参数列表中