我有一个递归函数,它将重复该函数,直到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) 尝试创建一个打印数组最高元素的递归函数.
它说,它需要;之前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)