当前位置:网站首页>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")
}
}
}
}
边栏推荐
- 自定义MVC(下)
- quartz2.2简单调度Job
- Data warehouse 4.0 notes - business data collection
- PHP文件上传中fileinfo出现的安全问题
- NFT digital collection development: Jingdong "Qida bear takes you to the capital" tourism package
- DVWA learning notes
- Dynamically set the theme color of the card
- sqli-lab 1-16通关随笔记
- Two sorting and one random data fetching of stored procedures
- php+码云 代码钩子自动更新线上代码
猜你喜欢

Development of digital collection system / introduction of digital collection scheme

数字藏品系统开发:NFT的主要特点有哪些?

MySQL account management & four engines & database and table building

NFT数字藏品平台开发搭建,源码开发数字藏品

Custom MVC (Part 1)

动态设置卡片的主题色

数字藏品系统开发:百度AI致敬中国航空

Compilation principle - detailed explanation of syntax analysis

NepCTF2022 Writeup

自定义MVC(上)
随机推荐
The difference between slice() and slice()
Data warehouse 4.0 notes - business data collection
NFT digital collection system development: Xu Beihong Art Museum unveiled through the digital collection platform
印尼央行行长称该国正在积极探索加密资产
sql实现连续登陆7天以上用户统计
kubesphere HA install (二)
MySQL之账号管理&&四大引擎&&建库建表
[monitoring deployment practice] display the charts of Prometheus and loki+promtail based on granfana
The 6th "Blue Hat Cup" National College Students' Cyber Security Skills Competition - preliminary writeup
Niuke question brushing record -- MySQL
数字藏品系统开发:企业布局元宇宙数字藏品
Digital collection development / digital collection system development solution
Genesis曾向三箭资本提供23.6亿美元的贷款
美联储理事沃勒:去中心化金融最终可能会改变传统金融市场
Development of digital collection system: what are the main features of NFT?
Web component - the lifecycle of a custom element
[pyautogui learning] screen coordinates and mouse scrolling
蚂蚁链NFT数字藏品DAPP商城系统定制开发
quartz2.2简单调度Job
Scala之一变量和数据类型