java - 我应该使用什么样的数组或列表来完成我的随机化代码?
问题描述
button.setOnClickListener {
//???
val rand = Random().nextInt()
phrase.text=rand.toString()}
到目前为止,这是我的代码,但我似乎找不到适合的数组或 listOf,它似乎总是以错误告终,代码有问题吗?我的目标是编写几个短语,单击按钮后将随机选择这些短语。
解决方案
如果我正确理解了你的问题,你可以使用类似的东西:
val random = Random(System.currentTimeMillis())
val list : List<String> = (1..10).map {
random.toString()
}
// now you can set the list of strings to whatever you want
// if you have an array of TextViews...
list.forEachIndexed { index, phrase ->
phrasesTextViews[index].text = phrase
}
但是如果你想要一个字符串中的所有短语:
phrasesTextView.text = (1..10).map { it.toString() }.joinToString(", ")
推荐阅读
- mysql - How to assign aliases to an array of column names with Joomla's quoteName()?
- reactjs - React Component 在我的状态下没有收到我的道具
- r - ggplot geom_boxplot 中间 aes 没有效果
- c# - 在两个服务之间转发 DTO 的序列化为 json 不好的做法
- r - R regular expression to parse call option code
- ruby-on-rails - 如何使用 Spree 在不同的 Rails 页面上导入不同的产品
- java - Java 中的自调用函数中的堆栈溢出错误(岛数)
- reactjs - 为什么我的反应路由器没有将我的快速路由传递给服务器?
- r - 将终端中的变量集导出到 R
- visual-studio - Outlook 加载项的静默安装