云星数据---Scala实战系列(精品版)】:Scala入门教程036-Scala实战源码-Scala match语句01

来源:互联网 发布:电气工程算量软件 编辑:程序博客网 时间:2024/06/08 16:26

Scala match语句

  • scala中的match语句用来在一个列表中选择某一个分支来执行分支的语句块,类似于其他语言中的swtich..case语句
package scala_learn.demo09_Match/** * Created by liguohua on 2017/3/1. */class O1_MatchDemo {}object O1_MatchDemo {  def main(args: Array[String]) {    test3()  }  //一个小用法  def test4(x: Int): String = x match {    case 1 => "one"    case 2 => "two"    case _ => "many"  }  def test3(): Unit = {    val v1 = 5    //模式匹配中可以直接进行方法调用    var rs = v1 match {      case 1 => "number one"      case 2 => "number two"      case 3 => "number three"      //可以直接使用语句块      case _ => {        println("please reinput ")        //最后一句作为块的返回值        "error number"      }    }    println(rs)  }  def test2(): Unit = {    val v1 = 5    //模式匹配中可以直接进行方法调用    var rs = v1 match {      case 1 => println("number one")      case 2 => println("number one")      case 3 => println("number three")      case _ => println("error number")    }    println(rs)  }  def test1(): Unit = {    val v1 = 5    //模式匹配有返回值,不需要break,匹配后直接返回(有返回值)    var rs = v1 match {      case 1 => "number one"      case 2 => "number two"      case 3 => "number three"      //模式匹配中可以直接使用语句块      case _ => "error number"    }    println(rs)  }}
阅读全文
0 0
原创粉丝点击