小编use*_*125的帖子

递归函数不返回Int

我有一个递归函数,它将重复该函数,直到if条件不满足,然后输出一个整数.但是,此函数外部需要整数的函数正在接收一个单元.我应该如何修改代码才能返回int?

count(r,c,1,0)

   def count(r: Int, c: Int, countR: Int, lalaCount: Int): Int = {
    if (countR < (r + 1)) count(r,c,countR + 1, lalaCount + countR)
    else (lalaCount + c + 1)
   }
Run Code Online (Sandbox Code Playgroud)

这是整个计划

object hw1 {
  def pascal(c: Int, r: Int): Int = {

   count(r,c,1,0)

   def count(r: Int, c: Int, countR: Int, lalaCount: Int): Int = {
    if (countR < (r + 1)) count(r,c,countR + 1, lalaCount + countR)
    else (lalaCount + c + 1)
   }
  } //On this …
Run Code Online (Sandbox Code Playgroud)

recursion functional-programming scala

2
推荐指数
1
解决办法
554
查看次数

递归方法不起作用.需要; ?

尝试创建一个打印数组最高元素的递归函数.

它说,它需要;之前else maxi=xs.head}之后max(xs.tail)

我不认为scala使用半冒号,你何时应该使用它们以及其他一些基本的句法规则.

var maxi = 0 
def max(xs: List[Int]): Int = {if (xs.isEmpty) throw new java.util.NoSuchElementException() 
      else if (xs.tail.isEmpty) maxi
      else if (xs.tail.head > xs.head) maxi = xs.tail.head
      max(xs.tail)
      else maxi=xs.head
      max(xs.tail)
}
Run Code Online (Sandbox Code Playgroud)

scala

-1
推荐指数
2
解决办法
4850
查看次数

标签 统计

scala ×2

functional-programming ×1

recursion ×1