标签:mamicode var 集合 for info 替换 lazy val chap
今日学习:scala
package com.chapter01.inputcon object forcon { def main(args: Array[String]): Unit = { print("to循环:") //[1,5] //这里i相当于val变量,可以写成纯函数 for (i <- 1 to 5) { print(i + " ") } println() print("集合循环:") //集合 val list = List("hello", 5, 10, "OK") for (item <- list) { print(item + " ") } println() print("until循环:") //[1,5) for (i <- 1 until 5) { print(i + " ") } println() print("循环守卫:") //循环守卫 for (i <- 1 to 5 if i != 3) { print(i + " ") } println() print("引入变量:") //引入变量 for (i <- 1 to 5; j = 5 - i) { print(j + " ") } println() print("嵌套循环:") //嵌套循环 for (i <- 1 to 3; j <- 1 to 3) { print(i + "|" + j + " ") } println() print("循环返回值:") //循环返回值 //可以对返回值i进行处理 var res = for (i <- 1 to 10) yield { if (i % 2 == 0) { i } else { "不是偶数" } } println(res) println("花括号可以替换小括号:") //花括号可以替换小括号 for (i <- 1 to 5; j = i * 2) { print(i + "|" + j + " ") } //等价于 println() for {i <- 1 to 5 j = i * 2} { print(i + "|" + j + " ") } println() print("控制步长----Range:") //Range参数:起点,终点,步数 for (i <- Range(1, 10, 2)) { print(i + " ") } println() print("控制步长----for循环守卫:") for (i <- 1 to 10 if i % 2 == 1) { print(i + " ") } } }
最近学习的动力不是很高,得想办法逼自己一把了。
标签:mamicode var 集合 for info 替换 lazy val chap
原文地址:https://www.cnblogs.com/20183711PYD/p/14390187.html