Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala阶乘语法错误_Scala_Factorial - Fatal编程技术网

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)}
。我真的不知道该怎么说,除了它肯定不是一个函数参数。这是一个表达式,你应该把它放在函数体中(在
=
之后),不在参数列表中