为什么此代码在 Kotlin Playground 或其他 IDE 中不起作用?

Why this code doesn't work in Kotlin Playground or other IDEs?

提问人:ecem 提问时间:3/27/2022 最后编辑:CommonsWareecem 更新时间:3/27/2022 访问量:208

问:

import kotlin.collections.maxByOrNull
import kotlin.test.*

fun main() {
    var inputArray = mutableListOf(3, 6, -2, -5, 7, 3)
    solution(inputArray)
}
fun solution(inputArray: MutableList<Int>): Int {
  return inputArray.zipWithNext(Int::times).maxOrNull() ?: 0
}

我试图在我的浏览器中测试这个答案,但我不能。

阵 列 列表 科特林 可变

评论

0赞 CommonsWare 3/27/2022
你期望代码做什么,你得到的结果是什么?

答:

2赞 ocos 3/27/2022 #1

您的代码正在工作,但您没有使用方法的结果。solution

fun main() {
    var inputArray = mutableListOf(3, 6, -2, -5, 7, 3)
    val output = solution(inputArray)

    // do something with output. At least print it to see
    println("output is $output") // output is 21
}

fun solution(inputArray: MutableList<Int>): Int {
  return inputArray.zipWithNext(Int::times).maxOrNull() ?: 0
}