1
我写了下面的代码:斯卡拉阶乘语法错误
def factorial(x: Int, factorial(x => { if (x == 0) 1 else x * factorial(x - 1) })): Int = factorial(3)
但得到的错误:
<console>:1: error: ':' expected but '(' found.
我写了下面的代码:斯卡拉阶乘语法错误
def factorial(x: Int, factorial(x => { if (x == 0) 1 else x * factorial(x - 1) })): Int = factorial(3)
但得到的错误:
<console>:1: error: ':' expected but '(' found.
你有factorial
为您的高清设置了一个param项目,这是不正确。尝试:
def factorial(x:Int): Int =
if (x == 0) 1
else x * factorial(x - 1)
的这部分代码是有道理的(语法,至少):
def factorial(...): Int = factorial(3)
在...
善有善报是函数的参数。你出发罚款x:Int
def factorial(x: Int, ...): Int = factorial(3)
但随后那种熄灭了导轨语法与factorial(x=>{if(x==0)1 else x*factorial(x-1)})
。我不知道该说什么,除了它绝对不是一个函数的参数。这是一个表达式,这是你放在函数体中的东西(在=
之后),而不是在参数列表中。