Scala 中简单表达式的非法开始 [英] Illegal start of simple expression in Scala

查看:83
本文介绍了Scala 中简单表达式的非法开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始学习Scala.我在 Eclipse 中尝试实现递归函数时遇到错误简单表达式的非法开始":

I just start learning scala. I got an error "illegal start of simple expression" in eclipse while trying to implement a recursive function:

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.

谁能告诉我在(递归)函数中定义变量时我做错了什么?我在网上进行了搜索,但无法解释此错误.

Can anyone tell me what I did wrong about defining a variable inside a (recursive) function? I did a search online but can't one explains this error.

推荐答案

变量声明 (var) 不返回值,因此您需要以某种方式返回值,代码如下可能看起来像:

A variable declaration (var) doesn't return a value, so you need to return a value somehow, here's how the code could look like:

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
    }

  }


}

这篇关于Scala 中简单表达式的非法开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆