Kotlin基础1

fun main(){
        //----------------Variables
//        val popcorn = 5
//        var a = 1
//        a = 2
//        println(a)
//        //----------------String
//        println("There are $popcorn customers")
//        println("There are ${popcorn+1} customers")
//
//        //----------------declared initialization
//        val d:Int
//        d = 3
//        val e:String="hello"
//        print(d)
//        println(e)
//        //----------------list
//        val readOnlyShapes = listOf<String>("triangle","square","circle")
//        println(readOnlyShapes)
//        var shapes:MutableList<String> = mutableListOf("triangle","square","circle")
//        shapes[0] = "tt"
//        println(shapes)
//        println("first : ${shapes.first()},last: ${shapes.last()}")
//        println("this list has ${shapes.count()} items")
//        println("circle" in shapes)
//        shapes.add("abc")
//        shapes.remove("abc")
//        println(shapes)
//        //----------------Set
//        var fruit:MutableSet<String> = mutableSetOf("a","a","b")
//        println(fruit)
//        //----------------Map
//        var accountBalances:MutableMap<Int,Int> = mutableMapOf(1 to 100,2 to 100,3 to 100)
//        println(accountBalances)
//        println(accountBalances.containsKey(2))
//        println(accountBalances.keys)
//        println(accountBalances.values)
//
//        println(200 in accountBalances.values)
//        //-----------------Control flow
//        val check = true
//        if(check)
//        {
//                a = 3
//        }else{
//
//                a=4
//        }
//        println(a)
//        val b = 2
//        println(if( a > b) a else b)
//        when("Hello")
//        {
//                "1" -> println("One")
//                "Hello" -> println("Greeting")
//                else -> println("Unknown")
//        }
//        val res = when("Hello")
//        {
//                "1" -> "One"
//                "Hello" -> "Greeting"
//                else -> "Unknown"
//
//        }
//        println(res)
//        val temp = 18
//        val desc = when
//        {
//                temp < 0 -> "very cold"
//                temp < 10 -> "a bit cold"
//                temp < 20 -> "warm"
//                else -> "hot"
//        }
//        println(desc)
        //---------------------Ranges Loop
        for(number in 1..5)
        {
                println(number)
        }

        val cakes = listOf("carrot","cheese","chocolate")
        for(cake in cakes)
        {
                println("Yummy, it's a $cake cake!")
        }
        var cakesEaten = 0
        while(cakesEaten < 3)
        {
                println("Eat a cake")
                cakesEaten++
        }
        //fizz buzz game
        for(number in 1..100)
        {
                println(when{
                        number % 15 == 0 -> "fizzbuzz"
                        number % 3 == 0 -> "fizz"
                        number % 5 == 0 -> "buzz"
                        else -> number.toString()

                })
        }
        //----------------Functions
//        hello()
//        println(sum(1,2))
//        printMessageWithPrefix("hello")
//        printMessageWithPrefix("hello","Error")
//        println(sum_s(1,2))

//        //-----------------lambda expression
        //1. lambda replace function,call itself
//        println({content:String -> content.uppercase()}("abc"))

        //2. assign to variable
//        val upperCaseString1: (String)->String = {t -> t.uppercase()}
//        println(upperCaseString1("ab"))

        //lambda == function
//        val upperCaseString = {str:String -> str.uppercase()}
        //3. pass lambda to filter function
//        val numbers = listOf(1,-2,3,-4, 5,-6)
//        val positives = numbers.filter { x-> x>0 }
//        val negatives = numbers.filter { x-> x<0 }
//        println(positives)
//        println(negatives)
        //4. map function to transform item in a collection
//        println(numbers.map{x -> x*2})
        //5. lambda return from a function
//        val timesInMinutes = listOf(2,10,15,1)
//        val min2sec = toSeconds("minute")
//        val totalTimeInSeconds = timesInMinutes.map(min2sec).sum()
//        println("Total time is $totalTimeInSeconds seconds")

}

猜你喜欢

转载自blog.csdn.net/zhuziying99/article/details/131930939