zoukankan      html  css  js  c++  java
  • scala (1) for 循环

    scala if  else 判断

    (1)在scala中末尾不需要添加 分号 作为语句的终结符。  val  name = "Leo"

      (2)  在 scala 中 if else 是有返回值的,返回值是最后一条语句。if(num > 10)"Li" else 2

      (3) 因为 if 和 else 是有值的 所以可以直接将 if 和 else 的结果复制给某个变量  val  name = if(num > 10)"Li" else 2    因为返回值类型不一样  所以推断其公共父类型是 any

      (4) 在 scala中 无论是方法还是函数 以及条件判断等 只要是有返回值的 都不需要加 return 关键字  val name = if (num > 20) "zs" else "lisi"

    (5)如果 没有else 的语句块,则对else 结果推断为 unit 即 没有else 返回值  会返回  ()  类似于  java 的 void

    if else 练习 :

    import scala.io.StdIn
    // import scala.util.control.Breaks._
    object BaseDemo {
      def main(args: Array[String]): Unit = {
       val userName = StdIn.readLine("please write your username" + "
    ")
       val passWord = StdIn.readLine("please write your password" + "
    ")
        if(userName.equals("admin") && passWord.equals("root")){
          println("welcome somebody")
        }else{
          println("get out")
        }
      }
    }

    scala  for 循环

    (1)使用 to 会产生一个连续不断的区间范围  状态:左右都是闭区间

    object BaseDemo {
      def main(args: Array[String]): Unit = {
        val a = 1 to 10
        println( a)
    
        for ( i <- 1 to 10){
          print(i)
        }
      }
    }
    // Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    //12345678910

    (2)使用until 会产生一个连续不间断的区间  状态:左闭右开

    object BaseDemo {
      def main(args: Array[String]): Unit = {
       for(i <- 0 until 10){
         print(i)
       }
      }
    }
    // 0123456789

    (3)for循环遍历字符串

    object BaseDemo {
      def main(args: Array[String]): Unit = {
          for(i<- "pwefjpwfpwkf[w"){
            // println(i)
            print(i + ",")
          }
      }
    }
    // p,w,e,f,j,p,w,f,p,w,k,f,[,w,

    (4)多重for循环遍历  九九乘法表

     def main(args: Array[String]): Unit = {
          for(x <- 1 to 9 ; y <- 1 to 9){
            if (y == 9){
              println(x + "*" + y + "=" + x * y )
            }else
            print(x + "*" + y + "=" + x * y + " 	 ")
          }
      }
    }
     /*
     1*1=1      1*2=2      1*3=3      1*4=4      1*5=5      1*6=6      1*7=7      1*8=8      1*9=9
     ...
     9*1=9      9*2=18      9*3=27      9*4=36      9*5=45      9*6=54      9*7=63      9*8=72      9*9=81
     */

    (5)带 if 守卫的 for 循环

    object BaseDemo {
      def main(args: Array[String]): Unit = {
         for(i <- 1 to 10 ){
             if (i % 2 == 0)  print (i + ",")  else ()
           }
      }
    }
    // 2,4,6,8,10,

    (6)推导式 for 循环  yield 会拿到每一个元素,然后对其进行 i * 3 的操作

    object BaseDemo {
      def main(args: Array[String]): Unit = {
         val arr = for( i <- 1 to 5 ) yield i *  3
        for(j <- arr){
          print(j + ",")
        }
      }
    }
    // 3,6,9,12,15,

    (7)for 循环遍历数组 Array

    object BaseDemo {
      def main(args: Array[String]): Unit = {
      val arr = Array(1,"hello","a")
        for(a <- arr){
          print(a + ",")
        }
      }
    }
     // 1,hello,a,

    (8) 中断 for循环 

    import scala.util.control.Breaks._
    
    object BaseDemo {
      def main(args: Array[String]): Unit = {
        breakable({
          for (i <- 0 to 10) {
            print (i + ",")
            if (i >= 5) {
              break()
            }
          }
        })
        }
    }
    // 0,1,2,3,4,5,

    (9)if 守卫 中断 for循环

    object BaseDemo {
      def main(args: Array[String]): Unit = {
       for(i <- 0 to 10){
          if(i <= 5){
            print(i + ",")
          }
       }
      }
    }
    // 0,1,2,3,4,5,
  • 相关阅读:
    Oracle修改数据文件路径
    检查交换空间: 可用的交换空间为 0 MB, 所需的交换空间为 150 MB。 未通过 <<<<
    Oracle 字符集修改
    Bad check value found during backing up datafileBad check value found during backing up datafile
    Oracle编译失效对象
    带where查询条件的expdp/impdp
    impdp导入报错案例ORA00907建表缺失右括号
    数据库参数 PGA_AGGREGATE_LIMIT 限制进程大小
    oracle本地服务名配置说明
    返回起始日期到终止日期的日期数据
  • 原文地址:https://www.cnblogs.com/mlfh1234/p/9211860.html
Copyright © 2011-2022 走看看