一个非常基本的问题,将 String 连接到 Int 的正确方法是什么?
我是 Kotlin 的新手,想打印一个Integer
带有 String的值并收到以下错误消息。
for (i in 15 downTo 10){
print(i + " "); //error: None of the following function can be called with the argument supplied:
print(i); //It's Working but I need some space after the integer value.
}
Run Code Online (Sandbox Code Playgroud)
预期结果
15 14 13 12 11 10
你有几个选择:
1. 字符串模板。我认为这是最好的。它绝对像 2-st 解决方案,但看起来更好,并允许添加一些需要的字符。
print("$i")
Run Code Online (Sandbox Code Playgroud)
如果你想添加一些东西
print("$i ")
print("$i - is good")
Run Code Online (Sandbox Code Playgroud)
添加一些表达式将其放在括号中
print("${i + 1} - is better")
Run Code Online (Sandbox Code Playgroud)
2.toString
方法,其可用于在科特林任何对象。
print(i.toString())
Run Code Online (Sandbox Code Playgroud)
3. 类似 Java 的连接解决方案
print("" + i)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4980 次 |
最近记录: |