在Scala中非法启动简单表达式
发布时间:2020-12-16 08:57:34 所属栏目:安全 来源:网络整理
导读:我刚开始学习 scala.在尝试实现递归函数时,我在 eclipse中收到错误“非法启动简单表达式”: def foo(total: Int,nums: List[Int]): if(total % nums.sorted.head != 0) 0 else recur(total,nums.sorted.reverse,0)def recur(total: Int,nums: List[Int],ind
我刚开始学习
scala.在尝试实现递归函数时,我在
eclipse中收到错误“非法启动简单表达式”:
def foo(total: Int,nums: List[Int]): if(total % nums.sorted.head != 0) 0 else recur(total,nums.sorted.reverse,0) def recur(total: Int,nums: List[Int],index: Int): Int = var sum = 0 // ***** This line complained "illegal start of simple expression" // ... other codes unrelated to the question. A return value is included. 谁能告诉我在(递归)函数中定义变量我做错了什么?我在网上搜索过,但无法解释这个错误. 解决方法
变量声明(var)不返回值,因此您需要以某种方式返回值,以下是代码的外观:
object Main { def foo(total: Int,coins: List[Int]): Int = { if (total % coins.sorted.head != 0) 0 else recur(total,coins.sorted.reverse,0) def recur(total: Int,coins: List[Int],index: Int): Int = { var sum = 0 sum } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |