1

结果我总是得到“1”。:(
这个功能有什么问题?

    def power(base: Int, exp: Int): BigInt = {
        def _power(result: BigInt, exp: Int): BigInt = exp match {
            case 0 => 1
            case _ => _power(result*base, exp-1)
        }
        _power(1, exp)
    }
4

2 回答 2

7

你必须这样替换:case 0 => result

于 2012-11-22T22:51:02.560 回答
0

可能与 OP 无关,但我将此答案用作示例,并且此版本有效:

def power(base: Int, exp: Int): BigInt = {
    def _power(result: BigInt, exp: Int): BigInt = exp match {
        case 0 => 1
        case 1 => result
        case _ => _power(result*base, exp-1)
    }
    _power(base, exp)
}
于 2019-03-31T19:56:56.963 回答