kotlin
Today we will learn how to use Kotlin print functions and how to get and parse user input from console. Furthermore, we’ll look into Kotlin REPL.
今天,我们将学习如何使用Kotlin打印功能以及如何从控制台获取和解析用户输入。 此外,我们将研究Kotlin REPL。
Kotlin打印功能 (Kotlin Print Functions)
To output something on the screen the following two methods are used:
要在屏幕上输出某些内容,请使用以下两种方法:
- print() 打印()
- println() println()
The print
statement prints everything inside it onto the screen.
print
语句将其中的所有内容print
到屏幕上。
The println
statement appends a newline at the end of the output.
println
语句在输出末尾添加换行符。
The print statements internally call System.out.print
.
打印语句内部调用System.out.print
。
The following code shows print statements in action:
以下代码显示了运行中的打印语句:
fun main(args: Array<String>) {
var x = 5
print(x++)
println("Hello World")
print("Do dinasours still exist?\n")
print(false)
print("\nx is $x.")
println(" x Got Updated!!")
print("Is x equal to 6?: ${x == 6}\n")
}
To print a variable inside the print statement, we need to use the dollar symbol($) followed by the var/val name inside a double quoted string literal.
要在print语句中打印变量,我们需要在双引号字符串文字中使用美元符号($),后跟var / val名称。
To print the result of an expression we use ${ //expression goes here }
.
要打印表达式的结果,我们使用${ //expression goes here }
。
The output when the above code is run on the Kotlin Online Compiler is given below.
下面是在Kotlin在线编译器上运行上述代码时的输出。
转义文字和表达式 (Escape literals and expressions)
To escape the dollar symbol and let’s say treat ${expression} as a string only rather than calculating it, we can escape it.
要转义美元符号,并且说将$ {expression}仅当作字符串而不是对其进行计算,我们可以对其进行转义。
fun main(args: Array<String>) {
val y = "\${2 == 5}"
println("y = ${y}")
println("Do w