0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Josh Skeen, David Greenhalgh著 『Kotlinプログラミング』 章末問題に挑戦(第3章)

Last updated at Posted at 2020-12-08

 Josh Skeen, David Greenhalgh著『Kotlinプログラミング』には章末問題がある。残念なことに解答がないので、第2章に引き続き本を読み進めながら答案を記述する。
 もし回答に間違いや意見があれば、ぜひご指摘いただきたい。

第3章 章末問題 回答案

 3.12 チャレンジ! オーラを補強する

Game.kt
fun main() {
    val name = "ミゲール"
    var healthPoints = 89
    val isBlessed = true

    val karma = (Math.pow(Math.random(), (110 - healthPoints) / 100.0) * 20).toInt()
//    println("karma = $karma")     // karma値を表示

    // Aura
    val auraColor = when (karma) {
        // karma値に応じてauraColorを指定
        in 0..5 -> "赤"
        in 6..10 -> "橙"
        in 11..15 -> "紫"
        in 16..20 -> "緑"
        else -> "NONE"
    }

    val healthStatus = when (healthPoints) {
        100 -> "は、最高だぜ!"
        in 90..99 -> "は、かすり傷を負った。"
        in 75..89 -> if (isBlessed) {
            "は、ちょっと傷があるが、このくらいすぐに治るぜ!"
        } else {
            "は、ちょっと傷がある。"
        }
        in 15..74 -> "は、かなりヤラレテいるようだ"
        else -> "は、ひどい状態だ!"
    }

    // Player status
    println(
        "(後光: $auraColor) " +
                "(福者: ${if (isBlessed) "はい" else "いいえ"})"
    )
    println("$name$healthStatus")
}

3.13 チャレンジ! 変更可能な状態フォーマット

 p46の欄外の訳注に「たぶん文字列テンプレートと内挿を使うことになるでしょう」との記述があるが、私自身、以下の回答で「文字列テンプレート」は使ったが、「内挿」をどのように使うのか分からなかった(p43に「内挿」の説明があるが、どの式を評価すればいいのか分からない)。どのような式を評価すればいいのかわかる方がいれば、ご意見をお聞かせ願いたい。

Game.kt
fun main() {
    val name = "ミゲール"
    var healthPoints = 100
    val isBlessed = true

    val karma = (Math.pow(Math.random(), (110 - healthPoints) / 100.0) * 20).toInt()
//    println("karma = $karma")     // karma値を表示

    // Aura
    val auraColor = when (karma) {
        // karma値に応じてauraColorを指定
        in 0..5 -> "赤"
        in 6..10 -> "橙"
        in 11..15 -> "紫"
        in 16..20 -> "緑"
        else -> "NONE"
    }

    val healthStatus = when (healthPoints) {
        100 -> "は、最高だぜ!"
        in 90..99 -> "は、かすり傷を負った。"
        in 75..89 -> if (isBlessed) {
            "は、ちょっと傷があるが、このくらいすぐに治るぜ!"
        } else {
            "は、ちょっと傷がある。"
        }
        in 15..74 -> "は、かなりヤラレテいるようだ"
        else -> "は、ひどい状態だ!"
    }

    // Player status
    var h = "$name$healthStatus"
    val statusFormatString = "(HP: $healthPoints)(Aura:$auraColor) -> $h"
    println(statusFormatString)

//    println(
//        "(後光: $auraColor) " +
//                "(福者: ${if (isBlessed) "はい" else "いいえ"})"
//    )
}

(2020/12/08現在)

第2章チャレンジ!答案
第3章チャレンジ!答案
第4章チャレンジ!答案
第7章チャレンジ!答案
第8章チャレンジ!答案

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?