目录
1 while循环
1.1 while
语法
while (布尔表达式) {
statement
}
1.2 do…while
语法
do {
statement
} while (布尔表达式)
1.3 while循环实例
package com.summer.bigdata.scala.basic
object WhileApp {
def main(args: Array[String]): Unit = {
//while循环
var a = 1
while (a < 5) {
println(a)
a = a + 1
}
//do...while循环
var b = 1
do {
println(b)
b = b + 1
} while(b < 1)
}
}
/*输出结果
1
2
3
4
1
*/
2 for循环
2.1 常见for循环
package com.summer.bigdata.scala.basic
object ForApp {
def main(args: Array[String]): Unit = {
//for <- 迭代拆分
for (ele <- "summer") {
println(ele)
}
/*输出结果
s
u
m
m
e
r
*/
//for <- 1 to 5 前闭后闭区间[1,5]
for (i <- 1 to 5) {
println(i)
}
/*输出结果
1
2
3
4
5
*/
//for <- 1 until 5 前闭后开区间[1,5)
for (i <- 1 until 5) {
println(i)
}
/*输出结果
1
2
3
4
*/
}
}
2.2 for循环步长和倒序
package com.summer.bigdata.scala.basic
object ForApp {
def main(args: Array[String]): Unit = {
//for循环步长
for (i <- 1 to 10 by 2) {
println(i)
}
/*输出结果
1
3
5
7
9
*/
//for循环倒序:负数步长
for (i <- 5 to 1 by -1) {
println(i)
}
/*输出结果
5
4
3
2
1
*/
//for循环倒序:reverse
for (i <- 1.to(5).reverse) {
println(i)
}
/*输出结果
5
4
3
2
1
*/
}
}
2.3 for循环守卫
package com.summer.bigdata.scala.basic
object ForApp {
def main(args: Array[String]): Unit = {
//for循环守卫
for (i <- 1 to 10 if i % 2 == 0) {
println(i)
}
/*输出结果
2
4
6
8
10
*/
}
}
2.4 打印九九乘法表
package com.summer.bigdata.scala.basic
object ForApp {
def main(args: Array[String]): Unit = {
//嵌套循环打印九九乘法表
for (i <- 1 to 9) {
for (j <- 1 to i) {
print(s"$i * $j = ${i * j}\t")
if (i == j) println()
}
}
/*输出结果
1 * 1 = 1
2 * 1 = 2 2 * 2 = 4
3 * 1 = 3 3 * 2 = 6 3 * 3 = 9
4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16
5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25
6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36
7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49
8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64
9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81
*/
//双循环打印99乘法表
for(i <- 1 to 9;j <- 1 to i) {
print(s"$i * $j = ${i * j}\t")
if (i == j) println()
}
/*输出结果
1 * 1 = 1
2 * 1 = 2 2 * 2 = 4
3 * 1 = 3 3 * 2 = 6 3 * 3 = 9
4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16
5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25
6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36
7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49
8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64
9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81
*/
}
}
2.5 yield
package com.summer.bigdata.scala.basic
object ForApp {
def main(args: Array[String]): Unit = {
//for循环yieled
val s = for (ele <- "summer") yield ele.toString.toUpperCase + ele
println(s.mkString(""))
/*输出结果
SsUuMmMmEeRr
*/
//for循环yield
val i2 = for (i <- 1 to 10) yield i * 2
println(i2)
/*输出结果
Vector(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)
*/
}
}
2.6 break
package com.summer.bigdata.scala.basic
//引入Breaks
import scala.util.control.Breaks.{break,breakable}
object ForApp {
def main(args: Array[String]): Unit = {
//break的使用,确认是否是质数
val a = 10
var flag = true
breakable(
for (i <- 2 to a / 2) {
if (a % i == 0) {
println(s"$a 被 $i 整除,不是质数")
flag = false
break
}
}
)
println(flag)
/*
10 被 2 整除,不是质数
false
*/
}
}