我有两个函数可以求出数字的阶乘,一个函数使用整数,但第二个函数只使用短整型会产生错误。这是代码
var fac16i: Function2[Short, Short, Short] = new Function2[Short, Short, Short] {
@tailrec override def apply (x: Short, acc: Short = 1): Short = {
if (x.toShort <= 1.toShort) acc.toShort
else apply(x - 1, x * acc)
}
}
var fac32i: Function2[Int, Int, Int] = new Function2[Int, Int, Int] {
@tailrec override def apply (x:Int, acc:Int=1): Int = {
if (x<=1) acc
else apply(x-1, x * acc)
}
}有人能帮我一下吗。注意:我使用的是scala-native。
发布于 2020-11-25 08:35:32
将.toShort强制转换从不需要的地方移动到需要的地方。
if (x <= 1) acc
else apply((x-1).toShort, (x*acc).toShort)https://stackoverflow.com/questions/64996803
复制相似问题