【问题标题】:Convert int element to char and then to string Kotlin error?将 int 元素转换为 char 然后转换为字符串 Kotlin 错误?
【发布时间】:2021-10-05 15:14:43
【问题描述】:

我正在尝试将一个 int 元素传递给一个 char,然后再传递给一个字符串,但它不起作用(字符串什么也不显示)。这是我遇到问题的代码部分。任何帮助都非常有用,谢谢您

private fun setFicha(view: ImageView, posicionFila: Int, posicionColumna: Int){
       val texto = findViewById<TextView>(R.id.texto)
       if(tablero[posicionFila][posicionColumna] == '-') {
           total++
           tablero[posicionFila][posicionColumna] = total.toChar()
           texto.text = tablero[posicionFila][posicionColumna].toString()
       }
       checkGameOver()
   }

【问题讨论】:

  • 请提供有关您的问题的更多详细信息。你想在tablero 中存储什么?

标签: arrays kotlin type-conversion


【解决方案1】:

要将Char 转换回Int,请使用code 属性:

49.toChar() // => '1' (Char)
49.toChar().code // => 49 (Int)
49.toChar().code.toString() // => "49" (String)

Char 在 Kotlin 中的类型表示 16 位 Unicode 字符。代码 49 对应于字符“1”。

【讨论】:

  • 好的!非常感谢:)
猜你喜欢
  • 1970-01-01
  • 2018-09-18
  • 1970-01-01
  • 2015-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多