package com.hengheng.scalaimport scala.util.control.Breaks._import scala.util.Randomobject Application{ def main(args : Array[String]) { println("Hello, World!") println(addSayHelloFirst(5, 10)) val I = List(1, 2, 3) I.map((i) => { println("HI") println(i + 9) }) } def addSayHelloFirst(i : Int, j : Int) : Int = { println("Hello") i + j } def add(i : Int, j : Int) = i + j val add2 = add(2, _ : Int) println(add2(99)) def devide(i : Int)(j : Int) : Int = i / j val devideBy2 = devide(_ : Int)(2) println(devideBy2(100)) var _100Devide = devide(100)(_ : Int) println(_100Devide(10)) var k = 5 if(k > 10) { println("k is more than 10.") } else if(k < 0) { println("k is less than 0") } else { println("k is between 1 and 10") } var i = 0 var sum = 0 while(i < 10) { sum += i i += 1 } println(sum) sum = 0 for (i <- 1 to 9) { sum += i } println(sum) breakable { while(true) { val r = new Random() val i = r.nextInt(10) println("i == " +i) if(i == 5) { break } } }}
输出:
101
5010k is between 1 and 104545i == 9i == 0i == 4i == 9i == 3i == 1i == 2i == 1i == 3i == 5Hello, World!Hello15HI10HI11HI12