当前位置:网站首页>Scala II process control
Scala II process control
2022-07-23 11:42:00 【AA master Zhao】
1、 Branch control if-else
Examples : demand 1: demand : Enter the age , If the age is less than 18 year , The output “ childhood ”. If age is greater than or equal to 18 And less than or equal to 30, The output “ middle-aged ”, otherwise , Output “ The elderly ”
object TestIfElse {
def main(args: Array[String]): Unit = {
println("input age")
var age = StdIn.readInt()
if (age < 18){
println(" childhood ")
}else if(age>=18 && age<30){
println(" middle-aged ")
}else{
println(" The elderly ")
}
}
}
(2) demand 2:Scala in if else An expression actually has a return value , The specific return value depends on the The last line of the code body .
object TestIfElse {
def main(args: Array[String]): Unit = {
println("input age")
var age = StdIn.readInt()
val res :String = if (age < 18){
" childhood "
}else if(age>=18 && age<30){
" middle-aged "
}else{
" The elderly "
}
println(res)
}
}
(3)Java The ternary operator in can be if else Realization If braces {} There's only one line of logical code inside , Braces can be omitted . If you omit braces ,if Only works on the nearest line of logic code .
object TestIfElse {
def main(args: Array[String]): Unit = {
// Java
// int result = flag?1:0
// Scala
println("input age")
var age = StdIn.readInt()
val res:Any = if (age < 18) " childhood " else " adult "
" It doesn't work "
println(res)
}
}
2 For Cycle control
2.1 Range data cycle (To) Close back and forth
for(i <- 1 to 3){
print(i + " ")
}
println()
2.2 Range data cycle (Until) To close before and open after
for(i <- 1 until 3) {
print(i + " ")
}
println()
2.3 Cycle guard
Cycle guard , That is, circulation protection ( Also known as conditional judgment , The guards ). The protection type is true Then go inside the circulatory body , by false Then skip , Be similar to continue.
for(i <- 1 to 3 if i != 2) {
print(i + " ")
}
println()
2.4 Cycle step
demand : Output 1 To 10 All odd numbers within
for (i <- 1 to 10 by 2) {
println("i=" + i)
}
2.5 Nested loop
for(i <- 1 to 3; j <- 1 to 3) {
println(" i =" + i + " j = " + j)
}
2.6 Loop return value
Return the results processed during traversal to a new Vector Collection , Use yield keyword
val res = for(i <- 1 to 10) yield i
println(res)
2.7 Flashback printing
If you want to print a set of data in reverse order , It can be used reverse.
for(i <- 1 to 10 reverse){
println(i)
}
3 Cycle break
3.1 Scala Built in control structure, specially Removed break and continue, To better adapt Functional programming , It is recommended to use the functional style to solve break and continue The function of , Not a keyword .Scala Use in breakable Control structure to achieve break and continue function
import scala.util.control.Breaks._
object TestBreak {
def main(args: Array[String]): Unit = {
breakable {
for (elem <- 1 to 10) {
println(elem)
if (elem == 5) break
}
}
println(" Normal end of cycle ")
}
Loop traversal 10 All data within , Odd print , Even skip (continue)
object TestBreak {
def main(args: Array[String]): Unit = {
for (elem <- 1 to 10) {
if (elem % 2 == 1) {
println(elem)
} else {
println("continue")
}
}
}
}
边栏推荐
- NFT trading platform digital collection system | development and customization
- NepCTF2022 Writeup
- Data warehouse 4.0 notes - business data collection
- XML建模
- Basis of penetration test
- Typescript common types
- Preliminary study on DC-1 shooting range
- Typescript introduction
- 编译原理-语法分析详解
- Simple implementation of rectangular area block
猜你喜欢
随机推荐
Solve the problem that the time format of manually querying Oracle database is incorrect (date type)
Basis of penetration test
数字藏品开发/元宇宙数字藏品开发
mysql修改函数权限未生效
美联储理事沃勒:去中心化金融最终可能会改变传统金融市场
MySQL之函数&视图&导入导出
Sqli lab 1-16 notes with customs clearance
Web Component-自定义元素的生命周期
自定义forEach标签&&select标签实现回显数据
Php+ code cloud code hook automatically updates online code
Phxpaxos installation and compilation process
Genesis provided a loan of US $2.36 billion to Sanya capital
使用el-table懒加载树形表格时的注意点
Preliminary study on DC-1 shooting range
Development of digital collection system: enterprise layout meta universe digital collection
Customized development of ant chain NFT digital collection DAPP mall system
Command Execution Vulnerability and defense
Some operations of composer
DVWA learning notes
The tree form based on El table and JS xlsx realize the function of downloading excel (II)




![[deployment] cluster deployment and startup of presto-server-0.261.tar.gz](/img/37/1185b2321b003a7793c8c37891008c.png)




