云星数据---Scala实战系列(精品版)】:Scala入门教程016-Scala实战源码-Scala 判断语句 、循环与java的比较

183 篇文章 0 订阅
64 篇文章 1 订阅

Scala 判断语句 、循环与java的比较

package scala_learn.demo01_FunctionStatement

/**
 * Created by liguohua on 2017/2/28.
 */
class O2_StatementsDemo {

}

object O2_StatementsDemo {
  def main(args: Array[String]) {
    fun1
    println("*********************************************************************")
    fun2
    println("*********************************************************************")
    fun3
  }

  //用于解释if语句
  def fun1() {
    val a:Int = 1
    //scala中的if语句和java中基本一致,但是它是有返回值的,java中是没有的
    val b = if (a == 1) {
      10
    } else 20;
    println(b)
  }

  //用于解释while循环,scala中循环是没有break和continue的
  def fun2() {
    var i = 0
    while (i < 5) {
      print(i)

      // i++//错误,scala中没有++,--操作
      i = i + 1
    }
    println()
  }

  //用于解释for循环,scala中循环是没有break和continue的
  def fun3(): Unit = {

    println("****************************for[0,5]")
    //[0,5]
    for (i <- 0 to 5) {
      print(i)
    }
    println()
    println("****************************for[0,5]")
    //[0,5],0是个数字,是个对象,to是对象身上的函数,5是这个to函数的实参
    for (i <- 0.to(5)) {
      print(i)
    }
    println()
    println("****************************for[0,5)")
    //[0,5)
    for (i <- 0 until 5) {
      print(i)
    }
    println()
    println("****************************for[0,5)")
    //[0,5),0是个数字,是个对象,until是对象身上的函数,5是这个until函数的实参
    for (i <- 0.until(5)) {
      print(i)
    }
    println()
    println("****************************for[0,10]之间的偶数")
    //[0,5],scala中的for()内可以有0-N个if语句(守卫)
    for (i <- 0 to 10; if (i % 2 == 0)) {
      print(i)
    }
    println()
    println("****************************for[0,10]之间的大于2的偶数")
    //[0,5]
    for (i <- 0 to 10; if (i % 2 == 0); if (i > 2)) {
      print(i)
    }
    println()
    println("****************************for[0,10]之间的大于2小于8的偶数")
    //[0,5]
    for (i <- 0 to 10; if (i % 2 == 0); if (i > 2); if (i < 8)) {
      print(i)
    }
    println()
    println("****************************for[0,10]之间的大于2小于8的偶数")
    //[0,5]
    for (i <- 0 to 10; if (i % 2 == 0); if (i > 2 && i < 8)) {
      print(i)
    }
    println()
    println("****************************大致的for循环嵌套**********************************")
    for (i <- 0 to 5; j <- 0 to 4) {
      println("i=" + i + ",j=" + j)
    }
    println("****************************正方形打印**********************************")
    for (i <- 0 to 5) {
      for (j <- 0 to 5) {
        print("*")
      }
      println()
    }
    println("****************************for中的yield**********************************")
    val no = for (i <- "hello") yield i
    println("no=" + no) //no=hello
    val no2 = for (i <- 1 to 2) yield i.toChar
    println("no2=" + no2) //no2=Vector(,)
    val no3 = for (i <- "hello"; j <- 1 to 2) yield (i + j).toChar
    println("no3=" + no3) //no3=ijfgmnmnpq
    val no4 = for (i <- 1 to 2; j <- "hello") yield (i + j).toChar
    println("no4=" + no4) //no4=Vector(i, f, m, m, p, j, g, n, n, q)


  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值