当前位置:网站首页>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)
边栏推荐
- The evolution of mobile cross platform technology
- Tabbar configuration at the bottom of wechat applet
- POJ-2499 Binary Tree
- Pytoch uses torchnet Classerrormeter in meter
- Learn garbage collection 01 of JVM -- garbage collection for the first time and life and death judgment
- ZABBIX agent2 monitors mongodb templates and configuration operations
- 一款新型的智能家居WiFi选择方案——SimpleWiFi在无线智能家居中的应用
- ZABBIX agent2 monitors mongodb nodes, clusters and templates (official blog)
- Deep discussion on the decoding of sent protocol
- MySQL function
猜你喜欢
The relationship between the size change of characteristic graph and various parameters before and after DL convolution operation
July Huaqing learning-1
Take you two minutes to quickly master the route and navigation of flutter
Pytoch implements tf Functions of the gather() function
Summary of C language learning problems (VS)
[superhard core] is the core technology of redis
Select drop-down box realizes three-level linkage of provinces and cities in China
UNIX socket advanced learning diary -ipv4-ipv6 interoperability
Redis clean cache
Third party payment interface design
随机推荐
The relationship between the size change of characteristic graph and various parameters before and after DL convolution operation
Hexadecimal conversion summary
MySQL function
MySQL data table operation DDL & data type
UNIX socket advanced learning diary -ipv4-ipv6 interoperability
Storage Basics
One article tells the latest and complete learning materials of flutter
Image hyperspectral experiment: srcnn/fsrcnn
Matlab boundarymask function (find the boundary of the divided area)
Clear neo4j database data
Swift - add navigation bar
只是巧合?苹果 iOS16 的神秘技术竟然与中国企业 5 年前产品一致!
Select drop-down box realizes three-level linkage of provinces and cities in China
Simple production of wechat applet cloud development authorization login
C alarm design
How to clear floating?
ZABBIX agent2 monitors mongodb templates and configuration operations
[figure neural network] GNN from entry to mastery
Seven polymorphisms
IPv6与IPv4的区别 网信办等三部推进IPv6规模部署