Scala基础入门-代码碎片

时间:2021-01-13 13:43:43
import scala.util.control._
import java.util.Date
object Test {
def main(args: Array[String]) {
// var a = 10;
// var b = 20;
// var c = 25;
// var d = 25;
// println("a + b = " + (a + b) );
// println("a - b = " + (a - b) );
// println("a * b = " + (a * b) );
// println("b / a = " + (b / a) );
// println("b % a = " + (b % a) );
// println("c % a = " + (c % a) );
// var a = 10
// a=a - 10;
// if(a > 5){
// println("go to the test area....") // }else{
// println("not go to the test area"+a)
// } // Local variable declaration:
//var a = 10; // while loop execution
// while( a < 20 ){
// println( "Value of a: " + a );
// a = a + 1;
// } // do{
// println( "Value of a: " + a );
// a = a + 1;
// }while( a < 20 ) // for( a <- 1 until 10){
// println( "Value of a: " + a );
// } //val numList = List(true,false,true); // for loop execution with a collection
// for( a <- numList ){
// println( "Value of a: " + a );
// } var a = 0;
val numList = List(1,2,3,4,5,6,7,8,9,10);
// for loop execution with a yield
// var retVal = for{ a <- numList ;if a != 3; if a < 8
// }yield a // Now print returned values using another loop.
// for( x <- retVal){
// println( "Value of x: " + x );
// } //函数可以随便定义位置,可以在main方法里面,亦或外面都可以执行
// val loop = new Breaks;
// loop.breakable {
// for( a <- numList){
// println( "Value of a: " + a );
// if( a == 4 ){
// loop.break;
// }
// }
// }
// println( "After the loop" ); // def addInt( a:Int, b:Int ) : Int = {
// var sum:Int = 0
// sum = a + b // return sum
// } // def factorial(n: BigInt): BigInt = {
// if (n <= 1)
// 1
// else
// n * factorial(n - 1)
// } // PrintUtil("this is the function result:"+addInt(1,10)); // for (i <- 1 to 10)
// PrintUtil( "Factorial of " + i + ": = " + factorial(i) ) // def apply(f: Int => String, v: Int) = f(v) // def layout[A](x: A) = "[" + x.toString() + "]" // println( apply( layout, 10) ) // var userDir = () => { System.getProperty("user.dir") } // println( userDir ) val date = new Date
val logWithDateBound = log(date, _ : String) logWithDateBound("message1" )
Thread.sleep(1000)
logWithDateBound("message2" )
Thread.sleep(1000)
logWithDateBound("message3" ) var dates = new Date
log(dates, "message1hh" )
Thread.sleep(1000)
log(dates, "message2hh" )
Thread.sleep(1000)
log(dates, "message3hh" ) val fruit = "apples" :: ("oranges" :: ("pears" :: Nil))
val nums = Nil println( "Head of fruit : " + fruit.head )
println( "Tail of fruit : " + fruit.tail )
println( "Check if fruit is empty : " + fruit.isEmpty )
println( "Check if nums is empty : " + nums.isEmpty ) } def log(date: Date, message: String) = {
val datebak = new Date
println(datebak + "----" + message)
} def PrintUtil(strings : String) : Unit = {
println("======================top======================");
println(strings);
println("======================bottom======================");
}
}