scala matchase

25 阅读1分钟
package matchase

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

}
package matchase

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

    //1.如果能精准匹配成功 就会执行后面的代码
    //2.case _ 表示可以匹配任意的内容 一定要放在match 擦色的最后一条
    //3.case _ 不能省略 如果匹配不成功 就会报错

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

}