3
我正在處理黑客排名問題(https://www.hackerrank.com/challenges/eval-ex),我有點卡住了。斯卡拉類型錯誤
我有一個例外是不斷上來:
Solution.scala:12: error: type mismatch;
found : (Double, Double) => Double
required: (AnyVal, AnyVal) => AnyVal
(1 to 9).reduce((total: Double, current: Double) => Math.pow(exp,current)/factNonRecursive(current))
那我在這Scala代碼做錯了什麼?
全碼:
object Solution {
def factNonRecursive(num: Double): Double = {
(1 to (num.toInt)).foldLeft(1) ((a,b) => (a * b)) toDouble
}
def main(args: Array[String]) {
val sc = new java.util.Scanner (System.in);
var n = sc.nextInt();
var a0 = 1;
while(a0 < n){
var x = sc.nextDouble();
def e(exp: Double) = {
(1 to 9).reduce((total: Double, current: Double) => Math.pow(exp,current)/factNonRecursive(current))
}
println(e(x))
a0+=1;
}
}
}
你是最偉大的。 – noname