kotlin_Kotlin print(),println(),readLine(),Scanner,REPL

本文详细介绍了Kotlin的打印功能,包括`print()`和`println()`的使用,以及如何处理转义字符和表达式。此外,还讲解了从控制台获取用户输入的方法,如`readLine()`和`Scanner`类,并通过示例展示了如何读取和转换用户输入。最后,简要介绍了Kotlin的REPL环境及其在命令行编译器中的使用。
摘要由CSDN通过智能技术生成

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值