scala模式匹配

45 阅读1分钟
object scala04 {
    def main(args: Array[String]): Unit = {
      val xiaohong = (100,100,100)
      val xiaoming = (89,98)

      getScore(xiaohong)
      getScore(xiaoming)
      getScore(1,2,3,4)
    }

    def getScore(score:Any):Unit = {
      score match {
        case (a,b,c) => println(s"元组中有三个元素: a = ${a} b = ${b} c = ${c}")
        case (a,b) => println(s"元组中有两个元素: a = ${a} b = ${b}")
        case _ => println("未知")
      }
    }

}

image.png

object scala05 {
  def main(args: Array[String]): Unit = {
    val a=11

    a match {
      case x:Int if a>10 => println("a is greater than 10")
      case _ => println("a is not greater than 10")
    }
  }

}

image.png