1.分支控制 if-else
package com.test.chapter04
import scala.io.StdIn
object Test01_IfElse {
def main(args: Array[String]): Unit = {
println("请输入您的年龄:")
val age: Int = StdIn.readInt()
//1.单分支
if (age >= 18){
println("成年")
}
println("==========================================")
//2.双分支
if (age >= 18){
println("成年")
} else {
println("未成年")
}
println("==========================================")
//3.多分支
//分支语句的返回值
val reslut: String = if (age <= 6){
println("童年")
"童年"
} else if(age < 18){
println("青少年")
"青少年"
} else if(age < 35){
println("青年")
"青年"
} else if(age < 60){
println("中年")
"中年"
} else {
println("老年")
"老年"
}
println("result: " + reslut)
println("===============================================")
//java中三元运算符 a?b:c
val res: String = if (age >= 18){
"成年"
} else {
"未成年"
}
val res2 = if (age >= 18) "成年" else "未成年"
println("=============================================")
// 5. 嵌套分支
if (age >= 18){
println("成年")
if (age >= 35){
if (age >= 60){
println("老年")
} else {
println("中年")
}
} else {
println("青年")
}
} else {
println("未成年")
if (age <= 6){
println("童年")
} else {
println("青少年")
}
}
}
}
2.For控制循环
package com.test.chapter04
object Test02_ForLoop {
def main(args: Array[String]): Unit = {
//java for语法: for(int i = 0; i < 10; i++){System.out.println(i + ". hello world")}
//范围遍历
for (i <- 1 to 10){
println(i + ". hello world")
}
for (i <- 1.to(10)){
println(i + ". hello world")
}
println("=======================================")
// for (i <- Range(1, 10)){
// println(i + ". hello world")
// }
for (i <- 1 until 10){
println(i + ". hello world")
}
println("======================================")
// 2.集合遍历
for (i <- Array(12, 34, 53)){
println(i)
}
println("======================================")
// 3.循环守卫
for (i <- 1 to 10){
if (i != 5){
println(i)
}
}
for (i <- 1 to 10 if i != 5){
println(i)
}
println("==========================================")
//4. 循环步长
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 <- 1 to 10 reverse){
println(i)
}
println("-===============================================")
for (i <- 1.0 to 10.0 by 0.5){
println(i)
}
println("==============================================")
//5. 嵌套循环
for (i <- 1 to 3){
for (j <- 1 to 3){
println("i = " + i + ", j = " + j)
}
}
println("==========================================")
for (i <- 1 to 4; j <- 1 to 5){
println("i = " + i + ", j = " + j)
}
}
}
3.九九乘法表案例
package com.test.chapter04
//输出九九乘法表
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()
}
// 简写
for (i <- 1 to 9; j <- 1 to i){
print(s"$j * $i = ${i * j} t")
if (j == i) println()
}
println("=====================================")
//6. 引入变量
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)
}
println("=======================================")
//6. 循环返回值
val a = for (i <- 1 to 10){
println(i)
}
println("a = " + a)
println("=======================================")
val b = for (i <- 1 to 10) yield i * i
println("b = " + b)
}
}
九层塔案例
package com.test.chapter04
// 打印输出一个九层塔
object Test04_Practice_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 -1; spaces = 9 - i){
println(" " * spaces + "*" * stars)
}
println("======================================")
for (stars <- 1 to 17 by 2; spaces = (17 - stars) / 2){
println(" " * spaces + "*" * stars)
}
}
}
5.While-Loop循环
package com.jny.chapter04
object Test05_WhileLoop {
def main(args: Array[String]): Unit = {
//while
var a: Int = 10
while (a >= 1){
println("this is a while loop: " + a)
a -= 1
}
var b: Int = 0
do {
println("this is a do-while loop: " + b)
b -= 1
} while (b > 0)
}
}
6.循环中断
package com.test.chapter04
import scala.util.control.Breaks
import scala.util.control.Breaks._
object Test06_Break {
def main(args: Array[String]): Unit = {
//1. 采用抛出异常的方式,退出循环
try{
for (i <- 0 until 5){
if (i == 3)
throw new RuntimeException
println(i)
}
} catch {
case e: Exception => //什么都不做,只是退出循环
}
//2.使用scala中的breaks类的的break方法,实现异常的抛出和捕捉
Breaks.breakable(
for (i <- 0 until 5){
if (i == 3)
Breaks.break()
println(i)
}
)
breakable(
for (i <- 0 until 5){
if (i == 3)
break()
println(i)
}
)
println("这是循环外的代码")
}
}