samedi 15 janvier 2022

Using Kotlin, why does this code only work for 2 of the 3 possibilities?

When I run this on my phone, the button I tap only does 2 of the 3 possible things, and it changes depending on how I swap the order in the code. For example, usually the first option just doesn't do anything at all, but sometimes the first and last work, depending on how I rearrange the code. I have no idea what's going on. Here's my code that I'm using to do this.

    private fun changeScore() {
        when ((1..10).random()) {
            1 or 2 or 3 or 4 or 5-> win()
            6 ->  Toast.makeText(this, "neither", Toast.LENGTH_SHORT).show()
            7 or 8 or 9 or 10 -> loss()
        }
        balance.text = getString(R.string.score, coins.toString())
    }

    private fun loss() {
        coins --
        Toast.makeText(this, "loss", Toast.LENGTH_SHORT).show()
    }
    private fun win() {
        coins ++
        Toast.makeText(this, "win", Toast.LENGTH_SHORT).show()
    } 

when I tap the button, you should have a chance at loosing 1 coin or gaining 1 coin, and a 1 in 10 chance of nothing happening. Additionally, as a debug helper, all 3 options make a toast. Instead, one of the 3 options does nothing at all, no coins, and no toast. I've tried to just ignore the bug and rearrange it so the one that doesn't work is the one that only gives a toast, but I cant get that to work. How do I fix this so that all 3 are a possibility? Keep in mind, I only have less than a week of experience with Kotlin, so please explain things with that in mind because I'm relatively new to coding in general.




Aucun commentaire:

Enregistrer un commentaire