我有一个递归函数,将重复该函数,直到if条件不满足,然后输出一个整数。然而,这个函数之外需要一个整数的函数正在接收一个单位。我应该如何修改代码才能返回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)
}
这是整个程序
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 line eclipse is saying "Multiple markers at this line
//- type mismatch; found : Unit required: Int
//- type mismatch; found : Unit required: Int
pascal(3,4)
}
是什么让你觉得它正在接收一个'Unit'?你的函数被定义为返回一个'Int',所以你肯定会得到一个'Int',你是如何确定你得到'Unit'的? – adelbertc
我做了一个编辑。 Elipse告诉我这一点。 – user1656125
你应该考虑接受一些答案,如果他们帮助解决你的问题 –