zoukankan      html  css  js  c++  java
  • 寒假学习记录第三天

    1.if else

        /**

         * if else

         */

        val age =18 

        if (age < 18 ){

         println("no allow")

        }else if (18<=age&&age<=20){

         println("allow with other")

        }else{

         println("allow self")

        }

    2. for ,while,dowhile

      /**

         * tountil

         * 例:

         * 1 to 10 返回110Range数组,包含10

         * 1 until 10 返回110 Range数组 ,不包含10

         */

        

        println(1 to 10 )//打印 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

        println(1.to(10))//与上面等价,打印 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

        

        println(1 to (10 ,2))//步长为2,从1开始打印 ,1,3,5,7,9

        println(1.to(10, 2))

        

        println(1 until 10 ) //不包含最后一个数,打印 1,2,3,4,5,6,7,8,9

        println(1.until(10))//与上面等价

        

    println(1 until (10 ,3 ))//步长为2,从1开始打印,打印1,4,7

     

    1. 创建for循环

       /**

         * for 循环

         *

         */

        for( i <- 1 to 10 ){

          println(i)

        }

    1. 创建多层for循环

        //可以分号隔开,写入多个list赋值的变量,构成多层for循环

        //scala中 不能写count++ count-- 只能写count+

        var count = 0;

        for(i <- 1 to 10; j <- 1 until 10){

          println("i="+ i +", j="+j)

          count += 1

        }

        println(count);

        

        //例子: 打印小九九

        for(i <- 1 until 10 ;j <- 1 until 10){

          if(i>=j){

           print(i +" * " + j + " = "+ i*j+" ")

            

          }

          if(i==j ){

            println()

          }

          

        }

     

    1. for循环中可以加条件判断,可以使用分号隔开,也可以不使用分号

        //可以在for循环中加入条件判断

        for(i<- 1 to 10 ;if (i%2) == 0 ;if (i == 4) ){

          println(i)

    }

     

    1. scala中不能使用count++count只能使用count = count+1 count += 1
    2. for循环用yield 关键字返回一个集合
    3. while循环,while(){}do {}while()

        

        //for中的符合条件的元素通过yield关键字返回成一个集合

        val list = for(i <- 1 to 10  ; if(i > 5 )) yield i 

        for( w <- list ){

          println(w)

    }

     

       /**

         * while 循环

         */

        var index = 0 

        while(index < 100 ){

         println(""+index+"while 循环")

          index += 1 

        }

        index = 0 

        do{

         index +=1 

         println(""+index+"do while 循环")

    }while(index <100 )

  • 相关阅读:
    021.day21 反射 Class类 反射常用操作
    020.day20 线程概述 多线程优缺点 线程的创建 线程常用方法 生命周期 多线程同步
    019.day19 缓冲流 对象流 标准输入输出流
    018.day18 map集合如何实现排序 File类 IO流 字节流 字符流 编码
    017.day17 Map接口 克隆 treeSet集合排重缺陷
    016.day16 HashSet TreeSet 比较器Comparable Comparator
    015.day15
    014.day14
    013.day13
    线程
  • 原文地址:https://www.cnblogs.com/xuange1/p/12253565.html
Copyright © 2011-2022 走看看