我正在學習斯卡拉,但我不能往這個代碼對於斯卡拉
def adjacentElementsProduct(inputArray: Array[Int]): Int = {
var maxSoFar = 0
var maxHere = 0
//for (i <- 0:Int to (inputArray.length-1)) <- error
for (i <- 0 to inputArray.length-1) //<- error
{
if(maxHere * inputArray(i) > 0)
maxHere *= inputArray(i)
else
maxHere = 0
if(maxHere > maxSoFar)
maxSoFar = maxHere
maxSoFar
}
}
編譯結果低谷for循環:
(沒有的:int)
file.scala on line 6: error: type mismatch;
found : Unit
required: Int
for (i <- 0 to inputArray.length-1)
^
(附:智力)
file.scala on line 6: error: identifier expected but integer literal found.
for (i <- 0:Int to (inputArray.length - 1))
^
file.scala on line 19: error: ')' expected but '}' found.
}
^
有什麼不對?我如何解決它?
感謝很多,達維德
如果'maxHere'開始於'0',maxHere * inputArray(i)怎麼會''0'? – jwvh
是啊我的錯,我已經糾正它與1儘快爲循環工作 –