流程控制
分支控制 if-else
??让程序有选择的执行,分支有三种:单分支、双分支、多分支
单分支
if (条件表达式) {
执行代码块
}
说明:当条件表达式为true时,就会执行()的代码。
需求:输入人的年龄,如果该同志的年龄小于18岁,则输出“未成年”
双分支
if (条件表达式) {
执行代码块1
} else {
执行代码块2
}
说明:当条件表达式为true时,就会执行()的代码 当条件表达式为false时,就会执行else的代码
需求:输入人的年龄,如果该同志的年龄小于18岁,则输出“未成年”,否则输出“成年”
多分支
if (条件表达式1) {
执行代码块
} else if (条件表达式2) {
执行代码块
}else if (条件表达式3) {
执行代码块
}
...
else if (条件表达式n) {
执行代码块
} else {
执行代码块
}
说明:当条件表达式为true时,就会执行()的代码 当条件表达式为false时,就会执行else的代码
需求: 当年龄小于等于6时输出“童年” 当年龄小于18时输出“未成年” 当年龄小于35时输出“成年” 当年龄小于60时输出“中年” 否则输出“老年”
package day04
import scala.io.StdIn
object Test01_IfElse {
def main(args: Array[String]): Unit = {
println("请输入您的年龄:")
val age: Int = StdIn.readInt()
if (age >= 18) {
println("成年")
}
println("===============")
if (age >= 18) {
println("成年")
} else {
println("未成年")
}
println("=============")
if (age <= 6) {
println("童年")
} else if (age < 18) {
println("未成年")
} else if (age < 35) {
println("成年")
} else if (age < 60) {
println("中年")
} else {
println("老年")
}
}
}
输出结果:
请输入您的年龄:
24
成年
===============
成年
=============
成年
分支语句的返回值
package day04
import scala.io.StdIn
object Test01_IfElse {
def main(args: Array[String]): Unit = {
println("请输入您的年龄")
val age: Int = StdIn.readInt()
val result: String = if (age <= 6) {
println("童年")
"童年"
} else if (age < 18) {
println("未成年")
"未成年"
} else if (age < 35) {
println("成年")
"成年"
} else if (age < 60) {
println("中年")
"中年"
} else {
println("老年")
"老年"
}
println("result:" + result)
}
}
输出结果:
请输入您的年龄
24
成年
result:成年
package day04
import scala.io.StdIn
object Test01_IfElse {
def main(args: Array[String]): Unit = {
println("请输入您的年龄")
val age: Int = StdIn.readInt()
val result1: String = if (age <= 6) {
println("童年")
"童年"
} else if (age < 18) {
println("未成年")
"未成年"
} else if (age < 35) {
println("成年")
"成年"
} else if (age < 60) {
println("中年")
"中年"
} else {
println("老年")
"老年"
}
println("result1:" + result1)
}
}
输出结果:
请输入您的年龄
24
成年
result:()
package day04
import scala.io.StdIn
object Test01_IfElse {
def main(args: Array[String]): Unit = {
println("请输入您的年龄")
val age: Int = StdIn.readInt()
val result2: Any = if (age <= 6) {
println("童年")
"童年"
} else if (age < 18) {
println("未成年")
age
} else if (age < 35) {
println("成年")
age
} else if (age < 60) {
println("中年")
age
} else {
println("老年")
age
}
println("result2:" + result2)
}
}
输出结果:
请输入您的年龄
25
成年
result2:25
Any可以返回任何类型的返回值
Scala没有三元运算符
package day04
import scala.io.StdIn
object Test01_IfElse {
def main(args: Array[String]): Unit = {
println("请输入您的年龄")
val age: Int = StdIn.readInt()
val res: String = if (age >= 18) {
"成年"
} else {
"未成年"
}
println("res" + res)
println("============")
val res2 = if (age >= 18) "成年" else "未成年"
println("res2" + res2)
}
}
输出结果:
请输入您的年龄
25
res成年
============
res2成年
嵌套语句
package day04
import scala.io.StdIn
object Test01_IfElse {
def main(args: Array[String]): Unit = {
println("请输入您的年龄:")
val age: Int = StdIn.readInt()
if (age >= 18) {
println("成年")
if (age >= 35) {
if (age >= 60) {
println("老年")
} else {
println("中年")
}
}
} else {
println("未成年")
if (age <= 6) {
println("童年")
}
}
}
}
输出结果:
请输入您的年龄:
23
成年
For循环控制
??Scala也为for循环这一常见的控制结构提供了非常多的特性,这些for循环的特性被称为for推导式或for表达式。
范围数据循环(To)
for (i < -1 to 3) {
print(i + " ")
}
println()
-
i表示循环的变量,<-规定to -
i将会从 1-3循环,前后闭合 -
案例
需求:输出5句"靓仔,你好帅啊!"
package day04
object Test02_ForLoop {
def main(args: Array[String]): Unit = {
for (i <- 1 to 5) {
println(i + ".靓仔,你好帅啊!")
}
println("===============")
for (i <- 6.to(10)) {
println(i + ".靓仔,你好帅啊!")
}
println("===============")
for (i <- Range(1, 10)) {
println(i + ".靓仔,你好帅啊!")
}
println("===============")
for (i <- 1 until (10)) {
println(i + ".靓仔,你好帅啊!")
}
}
}
输出结果:
G:\Java\jdk\bin\java.exe "-javaagent:F:\IDEA2021.3.1qiye\IntelliJ IDEA 2021.3.1\lib\idea_rt.jar=54760:F:\IDEA2021.3.1qiye\IntelliJ IDEA 2021.3.1\bin" -Dfile.encoding=UTF-8 -classpath G:\java\jdk\jre\lib\charsets.jar;G:\java\jdk\jre\lib\deploy.jar;G:\java\jdk\jre\lib\ext\access-bridge-64.jar;G:\java\jdk\jre\lib\ext\cldrdata.jar;G:\java\jdk\jre\lib\ext\dnsns.jar;G:\java\jdk\jre\lib\ext\jaccess.jar;G:\java\jdk\jre\lib\ext\jfxrt.jar;G:\java\jdk\jre\lib\ext\localedata.jar;G:\java\jdk\jre\lib\ext\nashorn.jar;G:\java\jdk\jre\lib\ext\sunec.jar;G:\java\jdk\jre\lib\ext\sunjce_provider.jar;G:\java\jdk\jre\lib\ext\sunmscapi.jar;G:\java\jdk\jre\lib\ext\sunpkcs11.jar;G:\java\jdk\jre\lib\ext\zipfs.jar;G:\java\jdk\jre\lib\javaws.jar;G:\java\jdk\jre\lib\jce.jar;G:\java\jdk\jre\lib\jfr.jar;G:\java\jdk\jre\lib\jfxswt.jar;G:\java\jdk\jre\lib\jsse.jar;G:\java\jdk\jre\lib\management-agent.jar;G:\java\jdk\jre\lib\plugin.jar;G:\java\jdk\jre\lib\resources.jar;G:\java\jdk\jre\lib\rt.jar;F:\IDEADEMO\ScalaLearnDemo\target\classes;G:\java\scala\scala-2.12.11\lib\scala-library.jar;G:\java\scala\scala-2.12.11\lib\scala-parser-combinators_2.12-1.0.7.jar;G:\java\scala\scala-2.12.11\lib\scala-reflect.jar;G:\java\scala\scala-2.12.11\lib\scala-swing_2.12-2.0.3.jar;G:\java\scala\scala-2.12.11\lib\scala-xml_2.12-1.0.6.jar day04.Test02_ForLoop
1.靓仔,你好帅啊!
2.靓仔,你好帅啊!
3.靓仔,你好帅啊!
4.靓仔,你好帅啊!
5.靓仔,你好帅啊!
===============
6.靓仔,你好帅啊!
7.靓仔,你好帅啊!
8.靓仔,你好帅啊!
9.靓仔,你好帅啊!
10.靓仔,你好帅啊!
===============
1.靓仔,你好帅啊!
2.靓仔,你好帅啊!
3.靓仔,你好帅啊!
4.靓仔,你好帅啊!
5.靓仔,你好帅啊!
6.靓仔,你好帅啊!
7.靓仔,你好帅啊!
8.靓仔,你好帅啊!
9.靓仔,你好帅啊!
===============
1.靓仔,你好帅啊!
2.靓仔,你好帅啊!
3.靓仔,你好帅啊!
4.靓仔,你好帅啊!
5.靓仔,你好帅啊!
6.靓仔,你好帅啊!
7.靓仔,你好帅啊!
8.靓仔,你好帅啊!
9.靓仔,你好帅啊!
集合遍历
package day04
object Test02_ForLoop {
def main(args: Array[String]): Unit = {
for (i <- Array(12, 34, 56, 23)) {
println(i)
}
for (i <- List(12, 34, 56, 23)) {
println(i)
}
for (i <- Set(12, 34, 56, 23)) {
println(i)
}
}
}
运行结果:
12
34
56
23
12
34
56
23
12
34
56
23
循环守卫
for (i <- 1 to 3 if i != 2) {
println(i + " ")
}
for (i <- 1 to 3) {
if (1 != 2) {
println(i + " ")
}
}
package day04
object Test02_ForLoop {
def main(args: Array[String]): Unit = {
for (i <- 1 to 10) {
if (i != 5) {
println(i)
}
}
println("===============")
for (i <- 1 to 10 if i != 5) {
println(i)
}
}
}
输出结果:
1
2
3
4
6
7
8
9
10
===============
1
2
3
4
6
7
8
9
10
循环步长
步长不能为0
package day04
object Test02_ForLoop {
def main(args: Array[String]): Unit = {
for (i <- 1 to 10 by 2) {
println(i)
}
println("===============")
for (i <- 13 to 30 by 3) {
println(i)
}
println("===============")
for (i <- 30 to 13 by -2) {
println(i)
}
println("===============")
for (i <- 10 to 1 by -1) {
println(i)
}
println("===============")
for (i <- 1 to 10 reverse) {
println(i)
}
println("===============")
for (data <- 1.0 to 10.0 by 0.5) {
println(data)
}
}
}
输出结果:
1
3
5
7
9
===============
13
16
19
22
25
28
===============
30
28
26
24
22
20
18
16
14
===============
10
9
8
7
6
5
4
3
2
1
===============
10
9
8
7
6
5
4
3
2
1
===============
1.0
1.5
2.0
2.5
3.0
3.5
4.0
4.5
5.0
5.5
6.0
6.5
7.0
7.5
8.0
8.5
9.0
9.5
10.0
嵌套循环
for (i <-1 to 3; j <- 1 to 3) {
println("i = " + i + "j =" + j)
}
没有关键字,所以范围后一定要加;来隔断逻辑
for (i <- 1 to 3) {
for (j <-1 to 3) {
println("i = " + i + "j =" + j)
}
}
package day04
object Test02_ForLoop {
def main(args: Array[String]): Unit = {
for (i <- 1 to 3) {
for (j <- 1 to 3) {
println("i = " + i + " j =" + j)
}
}
println("===============")
for (i <- 1 to 3; j <- 1 to 3) {
println("i = " + i + " j =" + j)
}
}
}
输出结果:
i = 1 j =1
i = 1 j =2
i = 1 j =3
i = 2 j =1
i = 2 j =2
i = 2 j =3
i = 3 j =1
i = 3 j =2
i = 3 j =3
===============
i = 1 j =1
i = 1 j =2
i = 1 j =3
i = 2 j =1
i = 2 j =2
i = 2 j =3
i = 3 j =1
i = 3 j =2
i = 3 j =3
九九乘法表
package day04
object Test03_Practice_MulTable {
def main(args: Array[String]): Unit = {
for (i <- 1 to 9) {
for (j <- 1 to i) {
print(s"$j * $i = ${i * j}\t")
}
println()
}
println("=========================")
for (i <- 1 to 9; j <- 1 to i) {
print(s"$j * $i = ${i * j}\t")
if (j == i) println()
}
}
}
输出结果:
G:\Java\jdk\bin\java.exe "-javaagent:F:\IDEA2021.3.1qiye\IntelliJ IDEA 2021.3.1\lib\idea_rt.jar=55763:F:\IDEA2021.3.1qiye\IntelliJ IDEA 2021.3.1\bin" -Dfile.encoding=UTF-8 -classpath G:\java\jdk\jre\lib\charsets.jar;G:\java\jdk\jre\lib\deploy.jar;G:\java\jdk\jre\lib\ext\access-bridge-64.jar;G:\java\jdk\jre\lib\ext\cldrdata.jar;G:\java\jdk\jre\lib\ext\dnsns.jar;G:\java\jdk\jre\lib\ext\jaccess.jar;G:\java\jdk\jre\lib\ext\jfxrt.jar;G:\java\jdk\jre\lib\ext\localedata.jar;G:\java\jdk\jre\lib\ext\nashorn.jar;G:\java\jdk\jre\lib\ext\sunec.jar;G:\java\jdk\jre\lib\ext\sunjce_provider.jar;G:\java\jdk\jre\lib\ext\sunmscapi.jar;G:\java\jdk\jre\lib\ext\sunpkcs11.jar;G:\java\jdk\jre\lib\ext\zipfs.jar;G:\java\jdk\jre\lib\javaws.jar;G:\java\jdk\jre\lib\jce.jar;G:\java\jdk\jre\lib\jfr.jar;G:\java\jdk\jre\lib\jfxswt.jar;G:\java\jdk\jre\lib\jsse.jar;G:\java\jdk\jre\lib\management-agent.jar;G:\java\jdk\jre\lib\plugin.jar;G:\java\jdk\jre\lib\resources.jar;G:\java\jdk\jre\lib\rt.jar;F:\IDEADEMO\ScalaLearnDemo\target\classes;G:\java\scala\scala-2.12.11\lib\scala-library.jar;G:\java\scala\scala-2.12.11\lib\scala-parser-combinators_2.12-1.0.7.jar;G:\java\scala\scala-2.12.11\lib\scala-reflect.jar;G:\java\scala\scala-2.12.11\lib\scala-swing_2.12-2.0.3.jar;G:\java\scala\scala-2.12.11\lib\scala-xml_2.12-1.0.6.jar day04.Test03_Practice_MulTable
1 * 1 = 1
1 * 2 = 2 2 * 2 = 4
1 * 3 = 3 2 * 3 = 6 3 * 3 = 9
1 * 4 = 4 2 * 4 = 8 3 * 4 = 12 4 * 4 = 16
1 * 5 = 5 2 * 5 = 10 3 * 5 = 15 4 * 5 = 20 5 * 5 = 25
1 * 6 = 6 2 * 6 = 12 3 * 6 = 18 4 * 6 = 24 5 * 6 = 30 6 * 6 = 36
1 * 7 = 7 2 * 7 = 14 3 * 7 = 21 4 * 7 = 28 5 * 7 = 35 6 * 7 = 42 7 * 7 = 49
1 * 8 = 8 2 * 8 = 16 3 * 8 = 24 4 * 8 = 32 5 * 8 = 40 6 * 8 = 48 7 * 8 = 56 8 * 8 = 64
1 * 9 = 9 2 * 9 = 18 3 * 9 = 27 4 * 9 = 36 5 * 9 = 45 6 * 9 = 54 7 * 9 = 63 8 * 9 = 72 9 * 9 = 81
1 * 1 = 1
1 * 2 = 2 2 * 2 = 4
1 * 3 = 3 2 * 3 = 6 3 * 3 = 9
1 * 4 = 4 2 * 4 = 8 3 * 4 = 12 4 * 4 = 16
1 * 5 = 5 2 * 5 = 10 3 * 5 = 15 4 * 5 = 20 5 * 5 = 25
1 * 6 = 6 2 * 6 = 12 3 * 6 = 18 4 * 6 = 24 5 * 6 = 30 6 * 6 = 36
1 * 7 = 7 2 * 7 = 14 3 * 7 = 21 4 * 7 = 28 5 * 7 = 35 6 * 7 = 42 7 * 7 = 49
1 * 8 = 8 2 * 8 = 16 3 * 8 = 24 4 * 8 = 32 5 * 8 = 40 6 * 8 = 48 7 * 8 = 56 8 * 8 = 64
1 * 9 = 9 2 * 9 = 18 3 * 9 = 27 4 * 9 = 36 5 * 9 = 45 6 * 9 = 54 7 * 9 = 63 8 * 9 = 72 9 * 9 = 81
引入变量
for (i <- 1 to 3;j = 4 - i) {
println("i =" + i + " j =" + j)
}
- for推导式一行中有多个表达式时,所以要加;来隔断逻辑
- for推导式有一个不成文的约定:当for推导式仅包含单一表达式时使用圆括号,当包含多个表达式时,一般每行一个表达式,并用花括号代替圆括号,如下
for {
i <- 1 to 3
j = 4 - i
} {
println("i =" + i + " j =" + j)
}
package day04
object Test02_ForLoop {
def main(args: Array[String]): Unit = {
for (i <- 1 to 3) {
for (j <- 1 to 3) {
println("i = " + i + " j =" + j)
}
}
println("===============")
for (i <- 1 to 3; j <- 1 to 3) {
println("i = " + i + " j =" + j)
}
println("===============")
for (i <- 1 to 10) {
val j = 10 - i
println("i =" + i + " j =" + j)
}
println("===============")
for (i <- 1 to 10; j = 10 - i) {
println("i =" + i + " j =" + j)
}
}
}
输出结果:
i =1 j =9
i =2 j =8
i =3 j =7
i =4 j =6
i =5 j =5
i =6 j =4
i =7 j =3
i =8 j =2
i =9 j =1
i =10 j =0
===============
i =1 j =9
i =2 j =8
i =3 j =7
i =4 j =6
i =5 j =5
i =6 j =4
i =7 j =3
i =8 j =2
i =9 j =1
i =10 j =0
九层妖塔
package day04
object Test_04Practice_Pyramid {
def main(args: Array[String]): Unit = {
for (i <- 1 to 9) {
val stars = 2 * i -1
val spaces = 9 - i
println(" " * spaces + "*" * stars)
}
println("==========")
for (i <- 1 to 9;stars = 2 * i;spaces = 9 -i) {
println(" " * spaces + "*" * stars)
}
println("==========")
for (stars <- 1 to 17 by 2;spaces = (17 - stars) / 2) {
println(" " * spaces + "*" * stars)
}
}
}
输出结果:
G:\Java\jdk\bin\java.exe "-javaagent:F:\IDEA2021.3.1qiye\IntelliJ IDEA 2021.3.1\lib\idea_rt.jar=56097:F:\IDEA2021.3.1qiye\IntelliJ IDEA 2021.3.1\bin" -Dfile.encoding=UTF-8 -classpath G:\java\jdk\jre\lib\charsets.jar;G:\java\jdk\jre\lib\deploy.jar;G:\java\jdk\jre\lib\ext\access-bridge-64.jar;G:\java\jdk\jre\lib\ext\cldrdata.jar;G:\java\jdk\jre\lib\ext\dnsns.jar;G:\java\jdk\jre\lib\ext\jaccess.jar;G:\java\jdk\jre\lib\ext\jfxrt.jar;G:\java\jdk\jre\lib\ext\localedata.jar;G:\java\jdk\jre\lib\ext\nashorn.jar;G:\java\jdk\jre\lib\ext\sunec.jar;G:\java\jdk\jre\lib\ext\sunjce_provider.jar;G:\java\jdk\jre\lib\ext\sunmscapi.jar;G:\java\jdk\jre\lib\ext\sunpkcs11.jar;G:\java\jdk\jre\lib\ext\zipfs.jar;G:\java\jdk\jre\lib\javaws.jar;G:\java\jdk\jre\lib\jce.jar;G:\java\jdk\jre\lib\jfr.jar;G:\java\jdk\jre\lib\jfxswt.jar;G:\java\jdk\jre\lib\jsse.jar;G:\java\jdk\jre\lib\management-agent.jar;G:\java\jdk\jre\lib\plugin.jar;G:\java\jdk\jre\lib\resources.jar;G:\java\jdk\jre\lib\rt.jar;F:\IDEADEMO\ScalaLearnDemo\target\classes;G:\java\scala\scala-2.12.11\lib\scala-library.jar;G:\java\scala\scala-2.12.11\lib\scala-parser-combinators_2.12-1.0.7.jar;G:\java\scala\scala-2.12.11\lib\scala-reflect.jar;G:\java\scala\scala-2.12.11\lib\scala-swing_2.12-2.0.3.jar;G:\java\scala\scala-2.12.11\lib\scala-xml_2.12-1.0.6.jar day04.Test_04Practice_Pyramid
*
***
*****
*******
*********
***********
*************
***************
*****************
==========
**
****
******
********
**********
************
**************
****************
******************
==========
*
***
*****
*******
*********
***********
*************
***************
*****************
循环返回值
val res = for (i <- 1 to 10) yield i
println(res)
说明:将遍历过程中处理的结果返回到一个新Vector集合中,使用yield关键字。
注意:开发中很少使用
需求:将原数据中所有值乘以2,并把数据返回到一个新的集合中。
package day04
object Test02_ForLoop {
def main(args: Array[String]): Unit = {
val a: Unit = for (i <- 1 to 10) {
i
}
println("a =" + a)
println("===============")
val b = for (i <- 1 to 10) {
i
}
println("b =" + b)
println("===============")
val c = for (i <- 1 to 10) yield i
println("c =" + c)
println("===============")
val ints = for (i <- 1 to 10) yield i
val d = ints
println("d =" + d)
println("===============")
val e: IndexedSeq[Int] = for (i <- 1 to 10) yield i
println("e =" + e)
}
}
输出结果:
a =()
===============
b =()
===============
c =Vector(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
===============
d =Vector(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
===============
e =Vector(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
While 和do…While循环控制
While和do…While的使用和Java语言中用法相同。
While 循环控制
循环变量初始化
while(循环条件) {
循环体(语句)
循环变量迭代
}
说明:
package day04
object Test05_WhileLoop {
def main(args: Array[String]): Unit = {
var a: Int = 10
while (a >= 1) {
println("这是一个while循环: " + a)
a -= 1
}
var b: Int = 0
do {
println("这是一个do-while循环: " + b)
b -= 1
} while (b > 0)
}
}
输出结果:
这是一个while循环: 10
这是一个while循环: 9
这是一个while循环: 8
这是一个while循环: 7
这是一个while循环: 6
这是一个while循环: 5
这是一个while循环: 4
这是一个while循环: 3
这是一个while循环: 2
这是一个while循环: 1
这是一个do-while循环: 0
循环中断
??Scala 内置控制结构特地去掉了 break 和 continue,是为了更好的适应函数式编程,推荐使用函数式的风格解决break和continue的功能,而不是一个关键字。Scala中使用breakable控制结构来实现break和continue功能。
需求: 1、采用抛出异常的方式,退出循环 2、使用Scala中的Breaks类的break方法,实现异常的抛出和捕捉
package day04
import scala.util.control.Breaks
object Test06_Break {
def main(args: Array[String]): Unit = {
try {
for (i <- 0 until 5) {
if (i == 3)
throw new RuntimeException
println(i)
}
} catch {
case e: Exception =>
}
println("=================")
Breaks.breakable(
for (i <- 0 until 5) {
if (i == 3)
Breaks.break()
println(i)
}
)
println("这是循环外的代码")
}
}
运行结果:
0
1
2
=================
0
1
2
这是循环外的代码
breakable原码
def breakable(op: => Unit) {
try {
op
} catch {
case ex: BreakControl =>
if (ex ne breakException) throw ex
}
}
优化
导包 import scala.util.control.Breaks._
package day04
import scala.util.control.Breaks
import scala.util.control.Breaks._
object Test06_Break {
def main(args: Array[String]): Unit = {
breakable(
for (i <- 0 until 5) {
if (i == 3)
break()
println(i)
}
)
println("这是循环外的代码")
}
}
运行结果:
0
1
2
这是循环外的代码
到底啦!喜欢靓仔的文章就在下方献出评论吧!( ?? ω ?? )?
|