package com.hengheng.scala import scala.util.control.Breaks._ import scala.util.Random object 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
50
10
k is between 1 and 10
45
45
i == 9
i == 0
i == 4
i == 9
i == 3
i == 1
i == 2
i == 1
i == 3
i == 5
Hello, World!
Hello
15
HI
10
HI
11
HI
12
时间: 2024-10-24 11:27:24