我试图在 Android Studio 上制作一个 Dice Roller 应用程序,但是当我放置这个 Random() 时,我得到一个错误提示“无法创建抽象类的实例”请帮助我
package com.example.diceroller
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val rollbutton: Button = findViewById(R.id.roll_button)
rollbutton.setOnClickListener(
View.OnClickListener { diceRoll() }
)
}
private fun diceRoll() {
val resultText: TextView = findViewById(R.id.result_text)
val randomInt = Random().nextInt(6) + 1
resultText.text = randomInt.toString()
}
}
每当我点击按钮时,我希望输出在 6 到 1 之间随机
最佳答案
Random在Kotlin中是抽象的,但是它提供了一些可以直接使用的静态方法,像这样:
Random.nextInt(6)
关于java - 无法创建抽象类 Random kotlin 的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57021345/