当前位置:网站首页>Kotlin流程控制、循环
Kotlin流程控制、循环
2022-07-05 12:32:00 【好奇的菜鸟】
一、流程控制
1.if
val i = 1
if (i > 0) {
print("Big")
} else {
print("Small")
}使用if,赋值。
val i = 1
val message = if (i > 0) "Big" else "Small"
print(message)
fun getLength(text: String?): Int {
return if (text != null) text.length else 0
}上边的简写,Elvis 表达式;
fun getLength(text: String?): Int {
return text?.length ?: 0
}2.when
val i: Int = 1
when(i) {
1 -> print("一")
2 -> print("二")
else -> print("i 不是一也不是二")
}
val i: Int = 1
val message = when(i) {
1 -> "一"
2 -> "二"
else -> "i 不是一也不是二" // 如果去掉这行,会报错
}
print(message)二、循环
1、for
val oneToThree = 1..3
for (i in oneToThree) {
println(i)
}
for (i in 6 downTo 0 step 3) {
println(i)
}
for (i in 0..10) {
println(i) //012345678910
}
for (i in 0 until 10) {
println(i) //0123456789
}
for (i in 0..10 step 2) {
print(i) //0 2 4 6 8 10
}
//获取元素
var abc = listOf("a", "b", "c")
for (i in abc) {
print(i)
}
// 获取下标
for (index in abc.indices) {
print("$index") // 0 1 2
}
// 获取元素和下标
for ((index, value) in abc.withIndex()) {
print("$index:$value ") //0:a 1:b 2:c
}2、while
var i = 0
while (i <= 2) {
println(i)
i++
}
var j = 0
do {
println(j)
j++
} while (j <= 2)
边栏推荐
- Matlab label2idx function (convert the label matrix into a cell array with linear index)
- Four operations and derivative operations of MATLAB polynomials
- Instance + source code = see through 128 traps
- C language structure is initialized as a function parameter
- MySQL index - extended data
- Correct opening method of redis distributed lock
- Flutter2 heavy release supports web and desktop applications
- PXE启动配置及原理
- Experimental design - using stack to realize calculator
- Seven ways to achieve vertical centering
猜你喜欢

Learn memory management of JVM 01 - first memory

Yum only downloads the RPM package of the software to the specified directory without installing it

Pytoch monolayer bidirectional_ LSTM implements MNIST and fashionmnist data classification

C language structure is initialized as a function parameter

UNIX socket advanced learning diary - advanced i/o functions

Principle of universal gbase high availability synchronization tool in Nanjing University

Understand kotlin from the perspective of an architect

Master-slave mode of redis cluster

UNIX socket advanced learning diary -ipv4-ipv6 interoperability

Matlab struct function (structure array)
随机推荐
ZABBIX agent2 monitors mongodb nodes, clusters and templates (official blog)
ZABBIX monitors mongodb (template and deployment operations)
Understand kotlin from the perspective of an architect
How does MySQL execute an SQL statement?
UNIX socket advanced learning diary -ipv4-ipv6 interoperability
Want to ask, how to choose a securities firm? Is it safe to open an account online?
Swift - enables textview to be highly adaptive
Seven ways to achieve vertical centering
Matlab imoverlay function (burn binary mask into two-dimensional image)
MySQL regular expression
Hexadecimal conversion summary
Storage Basics
POJ-2499 Binary Tree
Correct opening method of redis distributed lock
Complete activity switching according to sliding
MySQL splits strings for conditional queries
信息服务器怎么恢复,服务器数据恢复怎么弄[通俗易懂]
图像超分实验:SRCNN/FSRCNN
Matlab struct function (structure array)
【ijkplayer】when i compile file “compile-ffmpeg.sh“ ,it show error “No such file or directory“.