開発環境
- macOS Mojave - Apple (OS)
- Windows 10 Pro (OS)
- IntelliJ IDEA CE(Community Edition) (IDE(統合開発環境))
- Kotlin (プログラミング言語)
Head First Kotlin: A Brain-Friendly Guide (Dawn Griffiths(著)、David Griffiths(著)、O'Reilly Media)のChapter 3(functions - Getting Out of Main)、BE the Compiler(73)の解答を求めてみる。
コード
fun main() {
println("Aは問題ない。")
println("B")
println(timesThree(10))
println(timesThree1(10))
println(timesThree2(10))
println("C.")
println(maxValue(arrayOf(5, 1, 4, 2, 3)))
println(maxValue(arrayOf(1, 5, 2, 4, 3)))
}
// B
fun timesThree(x: Int): Int {
var y = x * 3
return y
}
fun timesThree1(x: Int): Int {
return x * 3
}
fun timesThree2(x: Int) = x * 3
// C
fun maxValue(args: Array<Int>): Int {
var max = args[0]
var x = 1
while (x < args.size) {
var item = args[x]
max = if (max >= item) max else item
x += 1
}
return max
}入出力結果
Aは問題ない。 B 30 30 30 C. 5 5 Process finished with exit code 0
0 コメント:
コメントを投稿