scala中matchcase的介绍与使用

36 阅读4分钟

格式:

变量 match {

case 值一 => 代码1

case 值二 => 代码2

.......

case —— => 其他}

执行流程是:如果value匹配到了pattern1,就执行结果1,如果都没有匹配到,就执行 _ 对应的内容。

match case 的用法

1:类似于if else if 的这种多分支选择结构,精确匹配值

2:可以匹配元组元素的数量

3:可以匹配元素类型

4:可以匹配元素特征

5:样例类

6:变量声明中的模式匹配

matchcase的注意事项

1:case_ 是不可省略的,如果匹配不成功,且没有case,程序就会报错

2:case_ 必须写在最后面

代码演示:

package matchcase

object Demo02 {
  def main(args: Array[String]): Unit = {
    def getScore(score:Any):Unit = {
    score match {
      case (a,b,c) => println(s"三个成绩是$a,$b,$c")
      case (a,b) => println(s"两个成绩是$a,$b")
      case (a) => println(s"一个成绩是$a")
      case _ => println("未知")
    }
    }
      val stu1 = (100,98,84)
      val stu2 = (95,84)
      val stu3 = (88)
      getScore(stu1)
      getScore(stu2)
      getScore(stu3)

  }
}
package matchcase

object Demo03 {
  def main(args: Array[String]): Unit = {
    test(1)
    test(1.1)
    test("scala")
   def test(i:Any):Unit = {
     i match {
       case x :Int => println("int")
       case x:Double => println("double")
       case _ => println("other")
     }
   }
    }
}
package matchcase

object Demo04 {
  def main(args: Array[String]): Unit = {
    test(Array(1,2,3))
    test(Array(1,2,3,4))
    test(Array(2,1,2,3))
    test(Array(2,3,1,3))
    test(Array(1,1,1,1))
    def test(arr:Any) = {
      arr match {
        case Array(1,x,y) => println("arr是一个三位数组,第一位为1")
        case Array(1,x,y,z) => println("arr是一个四位数组,第一位为1")
        case Array(x,1,y,z) =>println("arr是一个四位数组,第二位为1")
        case Array(x,y,1,z) =>println("arr是一个四位数组,第三位为1")
        case Array(x,y,z,1) =>println("arr是一个四位数组,第四位为1")
        case _ => println("未知")

      }
    }
     }
}
package matchcase

object Demo05 {
  def main(args: Array[String]): Unit = {
    //定义一个样例类 => 圆形,有一个属性半径
    case class Circle(r:Double)
    //定义一个样例类 => 矩形,有两个属性,宽和高
    case class Rectangle(width:Double,height:Double)

    //定义一个方法用来求圆形或矩形的面积
    def getArea(obj:Any):Double = {
      obj match {
        case Circle(r) => 3.14 * r * r
        case Rectangle(width,height) => width * height
        case _ => 0.0
      }
    }
    println(getArea(Circle(2.0)))
    println(getArea(Rectangle(2.0,3.0)))
    println(getArea("scala"))
     }
}
package matchcase

object Demo06 {
  def main(args: Array[String]): Unit = {
    //定义三个变量,分别设置初始值为arr的前三个元素的值
    val arr = Array(1,2,3,4)
    var a = arr(0)
    var b = arr(1)
    var c = arr(2)
    println(a,b,c)
    //使用模式匹配,将arr的前三个元素分别赋值给a,b,c
    val Array(x,y,z,_*) = arr
    println(x,y,z)
  }
}

守卫语句紧跟在case模式之后,通过if关键字引入,格式如下:

value match {

   case pattern if guardCondition => result

   // 其他case语句

}

其中,value是要进行匹配的值,pattern是匹配模式,guardCondition是守卫条件(一个布尔表达式),result是当该case分支匹配成功时返回的结果。

package matchcase

object Demo07 {
  def main(args: Array[String]): Unit = {
    //知识点:守卫
    //case 模式 if 条件 =>执行的事情
    val a = 11
    a match {
      case a:Int if a>10 => println("a大于十 is int")
      case _ => println("a is not int")
    }
  }
}

例题:通过matchcase编写一个判断星座的方法

package matchcase

object Demo08 {
  def main(args: Array[String]): Unit = {
    println(get(10,24))
    println(get(8,23))
    //星座
    def get(month: Int, day: Int): String = {

      month match {
        case 1 => day match {
          case d if d >= 1 && d <= 19 => "摩羯座"
          case d if d >= 20 => "水瓶座"
        }
        case 2 => day match {
          case d if d >= 1 && d <= 18 => "水瓶座"
          case d if d >= 19 => "双鱼座"
        }
        case 3 => day match {
          case d if d >= 1 && d <= 20 => "双鱼座"
          case d if d >= 21 => "白羊座"
        }
        case 4 => day match {
          case d if d >= 1 && d <= 19 => "白羊座"
          case d if d >= 20 => "金牛座"
        }
        case 5 => day match {
          case d if d >= 1 && d <= 20 => "金牛座"
          case d if d >= 21 => "双子座"
        }
        case 6 => day match {
          case d if d >= 1 && d <= 21 => "双子座"
          case d if d >= 22 => "巨蟹座"
        }
        case 7 => day match {
          case d if d >= 1 && d <= 22 => "巨蟹座"
          case d if d >= 23 => "狮子座"
        }
        case 8 => day match {
          case d if d >= 1 && d <= 22 => "狮子座"
          case d if d >= 23 => "处女座"
        }
        case 9 => day match {
          case d if d >= 1 && d <= 22 => "处女座"
          case d if d >= 23 => "天秤座"
        }
        case 10 => day match {
          case d if d >= 1 && d <= 22 => "天秤座"
          case d if d >= 23 => "天蝎座"
        }
        case 11 => day match {
          case d if d >= 1 && d <= 21 => "天蝎座"
          case d if d >= 22 => "射手座"
        }
        case 12 => day match {
          case d if d >= 1 && d <= 21 => "射手座"
          case d if d >= 22 => "摩羯座"
        }
      }
    }
  }
}