2019年3月24日日曜日

開発環境

Head First Kotlin: A Brain-Friendly Guide (Dawn Griffiths(著)、David Griffiths(著)、O'Reilly Media)のChapter 2(basic types and variables - Being a Variable)、BE the Compiler(52)の解答を求めてみる。

コード

fun main() {
    println("A.")
    val hobbits =
        arrayOf("Frodo", "Sam", "Merry", "Pippin")

    var x = 0
    while (x < 4) {
        println("${hobbits[x]} is a good Hobbit name")
        x += 1
    }
    println()
    x = 0
    while (x < hobbits.size) {
        println("${hobbits[x]} is a good Hobbit name")
        x += 1
    }

    println("B.")
    val firemen = arrayOf(
        "Pugh", "Pugh", "Barney McGrew", "Cuthbert", "Dibble", "Grub"
    )
    var firemanNo = 0
    while (firemanNo < 6) {
        println("Fireman number of $firemanNo is ${firemen[firemanNo]}")
        firemanNo += 1
    }
}

入出力結果

A.
Frodo is a good Hobbit name
Sam is a good Hobbit name
Merry is a good Hobbit name
Pippin is a good Hobbit name

Frodo is a good Hobbit name
Sam is a good Hobbit name
Merry is a good Hobbit name
Pippin is a good Hobbit name
B.
Fireman number of 0 is Pugh
Fireman number of 1 is Pugh
Fireman number of 2 is Barney McGrew
Fireman number of 3 is Cuthbert
Fireman number of 4 is Dibble
Fireman number of 5 is Grub

Process finished with exit code 0

0 コメント:

コメントを投稿