Hello world 世界您好
编辑页面最后修改时间:2024 年 8 月 1 日
Hello world
世界您好
Basic types
基本类型
Collections
收集
Control flow
控制流
Functions
功能
Classes
类
Null safety
零安全
Here is a simple program that prints "Hello, world!":
这是一个打印“Hello, world!”的简单程序:
fun main() {
println("Hello, world!")
// Hello, world!
}
xxxxxxxxxx
fun main() {
println("Hello, world!")
// Hello, world!
}
In Kotlin: 在 Kotlin 中:
fun
is used to declare a functionfun
用于声明函数The
main()
function is where your program starts frommain()
函数是程序开始的地方The body of a function is written within curly braces
{}
函数的主体写在大括号{}
内println()
andprint()
functions print their arguments to standard outputprintln()
和print()
函数将其参数打印到标准输出
A function is a set of instructions that performs a specific task. Once you create a function, you can use it whenever you need to perform that task, without having to write the instructions all over again. Functions are discussed in more detail in a couple of chapters. Until then, all examples use the main()
function.
函数是执行特定任务的一组指令。创建函数后,您可以在需要执行该任务时使用它,而无需重新编写指令。在几章中将更详细地讨论函数。在此之前,所有示例都使用 main()
函数。
All programs need to be able to store data, and variables help you to do just that. In Kotlin, you can declare:
所有程序都需要能够存储数据,而变量可以帮助您做到这一点。在 Kotlin 中,您可以声明:
Read-only variables with
val
带有val
的只读变量Mutable variables with
var
使用var
的可变变量
note 注意
You can't change a read-only variable once you have given it a value.
只读变量一旦为其指定了值,就无法更改该变量。
To assign a value, use the assignment operator =
.
要赋值,请使用赋值运算符 =
。
For example: 例如:
fun main() {
//sampleStart
val popcorn = 5 // There are 5 boxes of popcorn
val hotdog = 7 // There are 7 hotdogs
var customers = 10 // There are 10 customers in the queue
// Some customers leave the queue
customers = 8
println(customers)
// 8
//sampleEnd
}
xxxxxxxxxx
val popcorn = 5 // There are 5 boxes of popcorn
val hotdog = 7 // There are 7 hotdogs
var customers = 10 // There are 10 customers in the queue
// Some customers leave the queue
customers = 8
println(customers)
// 8
tip 提示
Variables can be declared outside the
main()
function at the beginning of your program. Variables declared in this way are said to be declared at top level.
变量可以在程序开始时在main()
函数之外声明。以这种方式声明的变量被称为在顶级声明。
As customers
is a mutable variable, its value can be reassigned after declaration.
由于 customers
是一个可变变量,因此可以在声明后重新分配其值。
note 注意
We recommend that you declare all variables as read-only (
val
) by default. Declare mutable variables (var
) only if necessary.
我们建议您默认将所有变量声明为只读 (val
)。仅在必要时声明可变变量 (var
)。
It's useful to know how to print the contents of variables to standard output. You can do this with string templates. You can use template expressions to access data stored in variables and other objects, and convert them into strings. A string value is a sequence of characters in double quotes "
. Template expressions always start with a dollar sign $
.
了解如何将变量的内容打印到标准输出非常有用。您可以使用字符串模板执行此操作。您可以使用模板表达式来访问存储在变量和其他对象中的数据,并将它们转换为字符串。字符串值是用双引号括起来的字符序列”。
模板表达式始终以美元符号 $
开头。
To evaluate a piece of code in a template expression, place the code within curly braces {}
after the dollar sign $
.
若要计算模板表达式中的一段代码,请将代码放在美元符号 $
后面的大括号 {}
中。
For example: 例如:
fun main() {
//sampleStart
val customers = 10
println("There are $customers customers")
// There are 10 customers
println("There are ${customers + 1} customers")
// There are 11 customers
//sampleEnd
}
xxxxxxxxxx
val customers = 10
println("There are $customers customers")
// There are 10 customers
println("There are ${customers + 1} customers")
// There are 11 customers
For more information, see String templates.
有关详细信息,请参阅字符串模板。
You will notice that there aren't any types declared for variables. Kotlin has inferred the type itself: Int
. This tour explains the different Kotlin basic types and how to declare them in the next chapter.
您会注意到没有为变量声明任何类型。Kotlin 推断出类型本身:Int
。本教程将介绍不同的 Kotlin 基本类型以及如何在下一章中声明它们。
Complete the code to make the program print "Mary is 20 years old"
to standard output:
完成代码,使程序打印“Mary is 20 years old”
到标准输出:
fun main() {
val name = "Mary"
val age = 20
// Write your code here
}
xxxxxxxxxx
fun main() {
val name = "Mary"
val age = 20
// Write your code here
}
Example solution
{...}
Thanks for your feedback!