当前位置:网站首页>Kotlin introductory notes (III) kotlin program logic control (if, when)
Kotlin introductory notes (III) kotlin program logic control (if, when)
2022-07-05 09:14:00 【Meng Meng Mu Xi】
Preface : This tutorial is best done with JAVA Study on the basis of
One 、if Use of statements
kotlin There are two main ways to implement conditional statements in :if and when.
Same usage :
fun largerNumber(num1 : Int , num2 : Int) : Int{
var value = 0
if(num1 > num2) {
value = num1
} else {
value = num2
}
return value
}
Different usage :
fun largerNumber(num1 : Int , num2 : Int) : Int {
val value = if (num1 > num2){
num1
}else{
num2
}
return value
}
kotlin And java Of if The statements are roughly the same , but kotlin Of if Statement can have a return value !!!
But there is a problem with the above code ,kotlin It is the most concise language , The above code has too many unnecessary parts . stay kotlin in , We can use the following expression instead ( If you have unclear friends, you can read my first two blogs ):
fun largerNumber(num1 : Int,num2 : Int) = if(num1 > num2) num1 else num2
Does it look much more comfortable ? Hem ...,Kotlin The charm of is still behind !
Two 、When sentence
use when And those still in use switch...case Compared with the language of , It's so happy ...( Roast about what anti human can't get rid of break) Don't talk much , Here comes the example ...... Ah , This example is complete , Why don't you see it yet ....
Suppose you compile a function to query exam scores ( if sentence ):
fun getScore(name : String) = if (name == "Tom") {
86
} else if (name == "Jim") {
77
} else if (name == "Jack") {
95
} else if (name == "Lily") {
100
} else {
0
}
But when we use when when , You can compare :
fun getScore(name : String) = when (name) {
"Tom" -> 86
"Jim" -> 77
"Jack" -> 95
"Lily" -> 100
else -> 0
}
Is it refreshing ?
The format is as follows :
when( Any type of parameter ) {
Match the value -> { Perform logical }
}
When the execution logic has only one sentence , { } It can also be omitted . Of course , It's an exact match . In addition to precise matching , We also have type matching ... Examples are as follows :
fun checkNumber(num : Number) {
when (num) {
is Int -> println("num is Int")
is Double -> println("num is Double")
else -> println("num neither Int, Neither Double")
}
}
when There is another way to write a statement without parameters
Take the example just now :
fun getScore(name : String) = when {
name == "Tom" -> 86
name == "Jim" -> 77
name == "Jack" -> 95
name == "Jack" -> 100
else -> 0
}
Be careful ,Kotlin When judging whether strings or objects are equal, you can directly use == .( And Java Different )
In addition to this precise match ,when It can also cooperate with startsWith() Realization Fuzzy matching ( With Tom Everyone at the beginning 86 branch ):
fun getScore(name : String) = when {
// With Tom Everyone at the beginning 86 branch
name.startsWith("Tom") -> 86
name == "Jim" -> 77
name == "Jack" -> 95
name == "Jack" -> 100
}
Okay , That's all for this issue , Remember to pay attention if you like !!! RI Geng Zhong !!!
Kotlin The small white , I took notes while studying teacher Guolin's works . If there are any mistakes, please point out ! Thank you for reading !
Reference resources :
《 First line of code Android ( The third edition )》 --- Guo Lin
边栏推荐
- TF coordinate transformation of common components of ros-9 ROS
- Hi Fun Summer, play SQL planner with starrocks!
- [code practice] [stereo matching series] Classic ad census: (5) scan line optimization
- C#图像差异对比:图像相减(指针法、高速)
- Configuration and startup of kubedm series-02-kubelet
- 优先级队列(堆)
- Multiple linear regression (sklearn method)
- 一题多解,ASP.NET Core应用启动初始化的N种方案[上篇]
- Programming implementation of ROS learning 5-client node
- Programming implementation of subscriber node of ROS learning 3 subscriber
猜你喜欢
My experience from technology to product manager
Solutions of ordinary differential equations (2) examples
c语言指针深入理解
ROS learning 4 custom message
Nodejs modularization
OpenGL - Model Loading
Applet (use of NPM package)
Rebuild my 3D world [open source] [serialization-1]
Summary and Reflection on issues related to seq2seq, attention and transformer in hands-on deep learning
Svgo v3.9.0+
随机推荐
Ros- learn basic knowledge of 0 ROS - nodes, running ROS nodes, topics, services, etc
My life
File server migration scheme of a company
Blue Bridge Cup provincial match simulation question 9 (MST)
Causes and appropriate analysis of possible errors in seq2seq code of "hands on learning in depth"
notepad++
. Net service governance flow limiting middleware -fireflysoft RateLimit
Huber Loss
ROS learning 4 custom message
3D reconstruction open source code summary [keep updated]
Illustrated network: what is gateway load balancing protocol GLBP?
Nodejs modularization
Attention is all you need
什么是防火墙?防火墙基础知识讲解
Svg optimization by svgo
Programming implementation of ROS learning 2 publisher node
Explain NN in pytorch in simple terms CrossEntropyLoss
利用请求头开发多端应用
Information and entropy, all you want to know is here
Understanding rotation matrix R from the perspective of base transformation