在 Scala 中编写简单的递归函数时,我不断收到此错误。我错过了什么?
scala> def count(n1:Int, n1:Int) : List[Int] = (n1 < n2) ? List() : List(n1, count((n1 - 1), n2))
<console>:1: error: ';' expected but '(' found.
def count(n1:Int, n1:Int) : List[Int] = (n1 < n2) ? List() : List(n1, count((n1 - 1), n2))
最佳答案
在 Scala 中,三元运算符是 if
。因此,?
和 :
可以替换为常用的 if
和 else
关键字。
另外,n2
在哪里定义的?我会在 count
中猜这样 def count(n1:Int, n2:Int) : List[Int] = ...
关于Scala <控制台> :1: error: ';' expected but '(' found,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14373681/