scala: 模式匹配——基础使用

20 阅读3分钟

根据身份证号码,输出这个人的籍贯

根据一个值输出另一个与之对应的值,很容易就想到if else

基本代码:

package matchcase

object matchcase01 {
  // 42-> 湖北省
  // 52-> 四川省
  def main(args: Array[String]): Unit = {
//    val code=42
//    var province=""
//    if(code==42){
//      province="湖北省"
//    }else if(code==52){
//      province="四川省"
//    } else{
//      province="未知"
//    }
//    println(province)
    val code=12
    val province=code match{
      case 42=>"湖北省"
      case 52=>"四川省"
      case _=>" 未知"

    }
    println(province)
  }

}

match case的基本格式

格式:

code match{
  case 1=> println("one")
  case 2=> println("two")
  case _=> println("未知")
}
  1. 如果能精准匹配成功,就会执行后面的代码
  2. case_ 它表示可以匹配任意的内容。一定要放在matchcase的最后一条。
  3. case_不能省略:如果匹配不成功,就会报错。

根据数值,输出对应的英文的基本代码:

package matchcase

object matchcase02 {

  def main(args: Array[String]): Unit = {
    //输入1,输出单词one
    //输入2,输出单词two
    //输入3,输出单词three
    //输入其他,输出单词other

    // 1.如果能精准匹配成功,就会执行后面的代码
    // 2. case_ 它表示可以匹配任意的内容。一定要放在matchcase的最后一条。
    // 3.case_不能省略:如果匹配不成功,就会报错。

    // 1.从键盘获取数字
    val code=10
    // 2. 使用matchcase进行匹配
    code match{
      case 1=> println("one")
      case 2=> println("two")
      case _=> println("未知")
    }

  }

}

match case的使用

基本代码

package matchcase
/**
 * match case
 */

object matchcase03 {

  def processValue(value:Any):Unit={
    value match{
      case x:Int => println("Int")
      case x:String => println("String")
      case _ => println("other")
    }
  }

  def main(args: Array[String]): Unit = {
    val obj =10
    processValue(obj)


  }

}

高阶匹配的元组元素数量

匹配元素的个数的基本代码:

package matchcase
/**
 * match case
 * 1.匹配变量类型
 * 2. 匹配数据类型
 * 3. 匹配元素的个数
 */

object matchcase03 {

  def processValue(value:Any):Unit={
    value match{
      case x:Int => println("Int")
      case x:String => println("String")
      case _ => println("other")
    }
  }

  def processNum(value:Any):Unit={
    value match{
      case(a,b,c) => println(s"三个元素的元组,第一个元素是${a}")
      case(x,y) => println("两个元素的元组")
      case _ => println("其他")


    }
  }

  def main(args: Array[String]): Unit = {
//    val obj =10
//    processValue(obj)
    processNum((1,2))
    processNum((1,2,3))


  }

}

匹配数组元素个数,元素特征的基本代码:

package matchcase

/**
 * match case
 * 1.匹配变量类型
 * 2. 匹配数据类型
 * 3. 匹配元素的个数
 * 4. 匹配数组元素个数,元素特征
 */

object matchcase04 {

 def matchArray(arr:Any):Unit= {
   arr match {
     case Array(x, y) => println("匹配到有两个元素的数组", x, y)
     case Array(1, b, c) => println("匹配到有三个元素的数组,第一个元素是1")
     case Array(a, b, c) => println("匹配到有三个元素的数组", a, b, c)
     case Array(10, _*) => println("第一个元素是10,元素的个数有任意个")
     case _ => println("没有匹配到")
   }
 }



  def main(args: Array[String]): Unit = {
    val arr1=Array(1,2,3)
    val arr2=Array(2,2,3)
    val arr3=Array(10,2,3,1)
    matchArray(arr1)
    matchArray(arr2)
    matchArray(arr3)


  }

}

匹配案例类

基本代码

package matchcase

/**
 * match case
 * 1.匹配变量类型
 * 2. 匹配数据类型
 * 3. 匹配元素的个数
 * 4. 匹配数组元素个数,元素特征
 * 5. 匹配案例类
 */

object matchcase05 {
  case class Circle(r:Double)
  case class Rectangle(w:Double,h:Double)

 def calculateArea(shape:Any):Double= {
   shape match {
     case Circle(r) => 3.14*r*r
     case Rectangle(w,h) => w*h

     case _ => 0.0
   }
 }



  def main(args: Array[String]): Unit = {
    val circle=Circle(2.0)
    val rectangle=Rectangle(2.0,3.0)
    println(calculateArea(circle))
    println(calculateArea(rectangle))



  }

}

变量声明时的匹配的基本代码:

package matchcase

/**
 * match case
 * 1.匹配变量类型
 * 2. 匹配数据类型
 * 3. 匹配元素的个数
 * 4. 匹配数组元素个数,元素特征
 * 5. 匹配案例类
 * 6. 变量声明时的匹配
 */

object matchcase06 {


  def main(args: Array[String]): Unit = {
  val arr = Array(11,22,33,4,5)
    // 定义3个变量:x,y,z 保存数组中的前三个元素的值
//    val x=arr(0)
//    val y=arr(1)
//    val z=arr(2)
    val Array(x,y,z,_*)=arr
    println(x,y,z)
  }

}