1.if 语句
object Test { def main(args: Array[String]) { var x = 10; if( x < 20 ){ println("x < 20"); } } } 结果 x < 20
2.if...else 语句
object Test { def main(args: Array[String]) { var x = 30; if( x < 20 ){ println("x 小于 20"); }else{ println("x 大于 20"); } } } 结果 x 大于 20
3.if...else if...else 语句
object Test { def main(args: Array[String]) { var x = 30; if( x == 10 ){ println("X 的值为 10"); }else if( x == 20 ){ println("X 的值为 20"); }else if( x == 30 ){ println("X 的值为 30"); }else{ println("无法判断 X 的值"); } } } 结果 X 的值为 30
4.while 循环
object Test { def main(args: Array[String]) { // 局部变量 var a = 10; // while 循环执行 while( a < 20 ){ println( "Value of a: " + a ); a = a + 1; } } } 结果 value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15 value of a: 16 value of a: 17 value of a: 18 value of a: 19
5.do...while 循环
object Test { def main(args: Array[String]) { // 局部变量 var a = 10; // do 循环 do{ println( "Value of a: " + a ); a = a + 1; }while( a < 20 ) } } 结果 value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15 value of a: 16 value of a: 17 value of a: 18 value of a: 19
6.for循环
import scala.util.control.Breaks.{break, breakable} object HelloWord { def main(args: Array[String]) { // for 循环 breakable { for (a <- 1 to 10) { // 1 to 10 1到10列表 包含10 1 until 10 不包含10 if (a == 9) { break // 结束循环 } println("Value of a: " + a); } } } } 结果 Value of a: 1 Value of a: 2 Value of a: 3 Value of a: 4 Value of a: 5 Value of a: 6 Value of a: 7 Value of a: 8
7.for 使用 yield
object Test { def main(args: Array[String]) { var a = 0; val numList = List(1,2,3,4,5,6,7,8,9,10); // for 循环 var retVal = for{ a <- numList if a != 3; if a < 8 }yield a // 输出返回值 for( a <- retVal){ println( "Value of a: " + a ); } } } 结果 value of a: 1 value of a: 2 value of a: 4 value of a: 5 value of a: 6 value of a: 7
8.case的用法
object HelloWord { def main(args: Array[String]) { // 1.简单匹配,值匹配 val bools = List(true, false, "方法") for (bool <- bools) { bool match { case true => println("heads") case false => println("tails") case _ => println("something other than heads or tails (yikes!)") // 没匹配到 } } // 2.类型匹配 val sundries = List(23, "Hello", 8.5, 'q') for (sundry <- sundries) { sundry match { case i: Int => println("got an Integer: " + i) case s: String => println("got a String: " + s) case f: Double => println("got a Double: " + f) case other => println("got something else: " + other) } } // 3.根据顺序匹配 val willWork = List(1, 3, 23, 90) val willNotWork = List(4, 18, 52) val empty = List() for (l <- List(willWork, willNotWork, empty)) { l match { case List(_, 3, _, _) => println("Four elements, with the 2nd being '3'.") case List(_*) => println("Any other list with 0 or more elements.") } } // 4.case里面用 guard 的数组匹配 val tupA = ("Good", "Morning!") val tupB = ("Guten", "Tag!") for (tup <- List(tupA, tupB)) { tup match { case (thingOne, thingTwo) if thingOne == "Good" => println("A two-tuple starting with 'Good'.") case (thingOne, thingTwo) => println("This has two things: " + thingOne + " and " + thingTwo) } } // 5.对象深度匹配 case class Person(name: String, age: Int) val alice = new Person("Alice", 25) val bob = new Person("Bob", 32) val charlie = new Person("Charlie", 32) for (person <- List(alice, bob, charlie)) { person match { case Person("Alice", 25) => println("Hi Alice!") case Person("Bob", 32) => println("Hi Bob!") case Person(name, age) => println("Who are you, " + age + " year-old person named " + name + "?") } } // 6.正则表达式匹配 val BookExtractorRE = """Book: title=([^,]+),s+authors=(.+)""".r val MagazineExtractorRE = """Magazine: title=([^,]+),s+issue=(.+)""".r val catalog = List( "Book: title=Programming Scala, authors=Dean Wampler, Alex Payne", "Magazine: title=The New Yorker, issue=January 2009", "Book: title=War and Peace, authors=Leo Tolstoy", "Magazine: title=The Atlantic, issue=February 2009", "BadData: text=Who put this here??" ) for (item <- catalog) { item match { case BookExtractorRE(title, authors) => println("Book "" + title + "", written by " + authors) case MagazineExtractorRE(title, issue) => println("Magazine "" + title + "", issue " + issue) case entry => println("Unrecognized entry: " + entry) } } } }