日記
久々に寒い中バスを待った.そういう時間が好きだったのを思い出した.
今日書いたコード
when文(でいいんだっけ?)いつも文と式の使い分けで混乱する.
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) var rollButton: Button = findViewById(R.id.button) rollButton.setOnClickListener { rollDice() } } private fun rollDice() { val dice = Dice(6) val rollResult = dice.roll() val luckyNumber = 4 val rollResultTextView = findViewById<TextView>(R.id.rollResultTextView) val resultMessageTextView = findViewById<TextView>(R.id.resultMessageTextView) rollResultTextView.text = rollResult.toString() when (rollResult) { luckyNumber -> resultMessageTextView.text = "You win!" else -> resultMessageTextView.text = "You lose..." } } } class Dice(private val numSides: Int) { fun roll(): Int { return (1..numSides).random() } }
感想
矢印使うの見た目が面白い.クロージャは渡せるんだろうか.