示例一:循环中断的两种方法
object Homework01 { def main(args: Array[String]): Unit = { /* 100以内的数求和,求出当和 第一次大于20的当前数 */ var sum = 0 breakable { for (i <- 1 to 100) { sum += i if (sum > 20) { println("第一次大于20的当前数=" + i) break() } } } //除了上面的break机制来中断,我们也可以使用循环守卫实现中断 println("===========================") //见多识广 var loop = true var sum2 = 0 for (i <- 1 to 100 if loop == true) { sum2 += i if (sum2 > 20) { println("循环守卫实现中断 第一次大于20的当前数=" + i) loop = false } println("i=" + i) } } }示例二:单分支
object Demo01 { def main(args: Array[String]): Unit = { println("输入年龄") val age = StdIn.readInt() if (age > 18) { println("age > 18") } //小的技巧,如何查看某个包下包含的内容 //1.比如我们想看 scala.io 包有什么内容 //2.将光标放在 io上即可,输入ctrl +b //3.将光标放在 StdIn上即可,输入ctrl +b,看的是StdIn源码 scala.io.StdIn } }示例三:多分支
object Demo02 { def main(args: Array[String]): Unit = { val age = 6 if (age > 18) { println("age > 18") } else { println("age <= 18") } } }示例四:if 返回值的
object Demo03 { def main(args: Array[String]): Unit = { //如果大括号{}内的逻辑代码只有一行,大括号可以省略, 这点和java 的规定一样 if (5 > 4) { println("5>4") } //Scala中任意表达式都是有返回值的,也就意味着if else表达式其实是有返回结果的,具体返回结果的值取决于满足条件的代码体的最后一行内容 val age = 7 val res = if (age > 20) { println("hello age > 20") 9 + 10 "yes ok" } else { 7 } println("res=" + res) // yes ok } }练习一:单分支
object Exercise01 { def main(args: Array[String]): Unit = { /* 【选作】定义两个变量Int,判断二者的和,是否既能被3又能被5整除,打印提示信息 */ val num1 = 10 val num2 = 5 val sum = num1 + num2 if (sum % 3 == 0 && sum % 5 == 0) { println("能被3又能被5整除") } else { println("能被3又能被5整除 不成立~") } /* 判断一个年份是否是闰年,闰年的条件是符合下面二者之一:(1)年份能被4整除,但不能被100整除;(2)能被400整除 */ //定义一个变量保存年份 val year = 2018 if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) { println(s"${year} 是闰年...") } else { println(s"${year} 不是闰年") } } }练习二:多分支判断方程根
object Exercise02 { def main(args: Array[String]): Unit = { /* 求ax2+bx+c=0方程的根。a,b,c分别为函数的参数,如果:b2-4ac>0,则有两个解; b2-4ac=0,则有一个解;b2-4ac<0,则无解; [a=3 b=100 c=6] 提示1:x1=(-b+sqrt(b2-4ac))/2a X2=(-b-sqrt(b2-4ac))/2a 提示2:sqrt(num) 在 scala 包中(默认引入的) 的math 的包对象有很多方法直接可用. 思路的分析 1. 定义三个变量a,b,c 2. 使用多分支完成 3. 因为 b2-4ac会多次使用,因此我们可以先计算,并保持到变量中 4. 判断,写逻辑 */ val a = 3 val b = 100 val c = 6 val m = b * b - 4 * a * c var x1 = 0.0 var x2 = 0.0 if (m > 0) { x1 = (-b + sqrt(m)) / 2 * a x2 = (-b - sqrt(m)) / 2 * a println("有两个解 x1=" + x1.formatted("%.2f") + "x2=" + x2.formatted("%.2f")) } else if (m == 0) { x1 = (-b + sqrt(m)) / 2 * a println("有一个解 x1=" + x1) } else { println("无解..") } } }练习三:() 空值
object Exercise03 { def main(args: Array[String]): Unit = { var sumVal = 90 val result = if(sumVal < 20){ "结果大于20"//() } println("res=" + result) //返回的是() 即 Unit } }练习四:运动会(多分支)
object Exercise04 { def main(args: Array[String]): Unit = { /* 参加百米运动会,如果用时8秒以内进入决赛,否则提示淘汰。并且根据性别提示进入男子组或女子组。【可以让学员先练习下5min】, 输入成绩和性别,进行判断。 */ println("请输入运动员的成绩") val speed = StdIn.readDouble() if (speed <= 8) { println("请输入性别") val gender = StdIn.readChar() if (gender == '男') { println("进入男子组") } else { println("进入女子组") } } else { println("你被淘汰...") } } }练习五:够票(多分支)
object Exercise05 { def main(args: Array[String]): Unit = { /* 应用案例2 出票系统:根据淡旺季的月份和年龄,打印票价 [考虑学生先做5min] 4_10 旺季: 成人(18-60):60 儿童(<18):半价 老人(>60):1/3 淡季: 成人:40 其他:20 思路分析 1. 定义至少三个变量 month , age, ticket 2. 逻辑上有月份和年龄的判断因此,会使用嵌套分支 3. 根据对应的业务逻辑完成代码 走代码 */ println("输入月份") val month = StdIn.readInt() println("输入年龄") val age = StdIn.readInt() val tikcet = 60 if (month >= 4 && month <= 10) { if (age >= 18 && age <= 60) { println("你的票价是" + tikcet) } else if (age < 18) { println("你的票价是" + tikcet / 2) } else { println("你的票价是" + tikcet / 3) } } else { if (age >= 18 && age <= 60) { println("你的票价是" + 40) } else { println("你的票价是" + 20) } } } }练习六:考试奖励(多分支)
object ifelsesDemo03 { def main(args: Array[String]): Unit = { /* 岳小鹏参加scala考试,他和父亲岳不群达成承诺: 如果: 成绩为100分时,奖励一辆BMW; 成绩为(80,99]时,奖励一台iphone7plus; 当成绩为[60,80]时,奖励一个 iPad; 其它时,什么奖励也没有。 成绩是从控制台输入 */ println("请输入成绩") val score = StdIn.readDouble() if (score == 100) { println("成绩为100分时,奖励一辆BMW") } else if (score > 80 && score <= 99) { //写法1使用范围,写法2就是严格的判断 println("成绩为(80,99]时,奖励一台iphone7plus") } else if (score >= 60 && score <= 80) { println("奖励一个 iPad") } else { println("没有任何奖励") } } }练习七:评估班级成绩
object Exercise { /* 应用实例: 1.统计三个班成绩情况,每个班有5名同学,求出各个班的平均分和所有班级的平均分[学生的成绩从键盘输入]。 分析思路 (1) classNum 表示 班级个数 , stuNum 表示学生个数 (2) classScore 表示各个班级总分 totalScore 表示所有班级总分 (3) score 表示各个学生成绩 (4) 使用循环的方式输入成绩 2.统计三个班及格人数,每个班有5名同学。 3.打印出九九乘法表 */ def main(args: Array[String]): Unit = { val classNum = 3 val stuNum = 5 var score = 0.0 //分数 var classScore = 0.0 //班级的总分 var totalScore = 0.0 //所有班级总分 for (i <- 1 to classNum) { //先将 classScore 清0 classScore = 0.0 for (j <- 1 to stuNum) { printf("请输入第%d班级的第%d个学生的成绩\n", i, j) score = StdIn.readDouble() classScore += score } //累计 totalScore totalScore += classScore printf("第%d班级的平均分为%.2f\n", i, classScore / stuNum) } printf("所有班级的平均分为%.2f", totalScore / (stuNum * classNum)) } }练习八:统计三个班及格人数
object Exercise02 { def main(args: Array[String]): Unit = { //2.统计三个班及格人数,每个班有5名同学。 val classNum = 3 val stuNum = 5 var score = 0.0 //分数 var classScore = 0.0 //班级的总分 var totalScore = 0.0 //所有班级总分 var passNum = 0 //统计及格人数 for (i <- 1 to classNum) { //先将 classScore 清0 classScore = 0.0 for (j <- 1 to stuNum) { printf("请输入第%d班级的第%d个学生的成绩\n", i, j) score = StdIn.readDouble() if (score >= 60) { passNum += 1 } classScore += score } //累计 totalScore totalScore += classScore printf("第%d班级的平均分为%.2f\n", i, classScore / stuNum) } printf("所有班级的平均分为%.2f", totalScore / (stuNum * classNum)) printf("所有班级的及格人数为%d", passNum) } }练习九:打印九九乘法
object Exercise03 { def main(args: Array[String]): Unit = { //3.打印出九九乘法表 //思路分析 //(1) 使用两层循环, 有9行, 每1行的列数在增加 //(2) 根据逻辑,我们可以编写代码 val num = 9 for (i <- 1 to num) { //确定行数 for (j <- 1 to i) {//确定列数 printf("%d * %d = %d\t" , j , i , i * j) } println() } } }