问题导入
我们今天接到一个开发任务,就是根据身份证号码,输出这个人的籍贯。例如:42表示湖北,11表示北京
object day45 {
/*
match case
* 三大结构
* 1. 顺序结构:
* 2. 选择结构:
* (1) if,if else if else;
* (2) match case
* 1. case _ 不能省略,否则,匹配失败会报错。
* 2. case _ 必须写在最后。
* 3. 循环结构:
* (2) for,
* (3) while,do...while
*/
def main(args: Array[String]): Unit = {
// if(code=="42"){
// privince="湖北"
// }else if(code=="11"){
// privince="北京"
// }else{
// privince="未知"
// }
val code="42"
val province=code match{
case "42"=>"湖北"
case "11"=>"北京"
case _=>"未知"
}
println(s"${code}对应的省份:${province}")
}
}
match case的基本格式
value match {
case pattern1 => result1
case pattern2 => result2
...
case patternN => resultN
case _ => 其他
}
执行流程是:如果value匹配到了pattern1,就执行结果1,如果都没有匹配到,就执行 _ 对应的内容
案例:
val num = 5
val result = num match {
case 1 => "One"
case 2 => "Two"
case 3 => "Three"
case 4 => "Four"
case 5 => "Five"
case _ => "Other"
}
println(result)
案例:不同的水果宣传语
val str = "apple"
val output = str match {
case "apple" => "It's an apple."
case "banana" => "It's a banana."
case _ => "It's some other fruit."
}
高阶匹配之元组元素数量
前面的匹配都是精准匹配:变量和值是相等的,scala中的模式匹配的规则是非常强大的,不一定是精准的匹配值,还可以根据元组的元素的个数来匹配
object day47 {
/*
match case 高阶匹配
1. 匹配元组不同的元素的数量
2.匹配数组特殊值
*/
def main(args: Array[String]): Unit = {
// 元组
val t1 = (1,2)
t1 match {
case (a,b) => println(s"有二个元素${a}, ${b}")
case _ => println("未知")
}
val arr1=Array(1,2,3)
arr1 match{
case Array(1,x,y)=>println("属组,第一个元素是1,长度是3")
case _=>println("其他")
}
}
}
高阶匹配之变量类型匹配
格式如下:
对象名 match {
case 变量名1:类型1 => 表达式1
case 变量名2:类型2 => 表达式2
case 变量名3:类型3 => 表达式3
// 其他case语句...
casee _ => 表达式
}
示例代码:
object day48 {
/*
match case 高阶匹配
1. 匹配元组不同的元素的数量
2.匹配数组特殊值
3.匹配变量的类型
*/
def matchType(value:Any):Unit={
value match{
case x:Int=>println(s"${x}是Int")
case x:Double=>println(s"${x}是Double")
case _=> println("未知")
}
}
def main(args: Array[String]): Unit = {
matchType(1)
matchType(1.1)
}
}
高阶匹配之匹配样例类(属性值)
格式:
对象 match {
case 样例类类型1(字段1,字段2)=> 表达式
case 样例类类型2(字段1,字段2,字段3)=> 表达式
case _ => 其他
}
✨注意:样例类1中的字段个数要与之对应
示例代码如下:
object day49 {
/*
match case 高阶匹配
1. 匹配元组不同的元素的数量
2.匹配数组特殊值
3.匹配变量的类型
4.匹配样例类(属性值)
*/
// 1. 圆形类
case class Circle(radius: Double) {}
// 2. 矩形类
case class Rectangle(width: Double, height: Double) {}
// 封装一个求面积的方法
def getArea(shape: Any): Double = {
shape match {
case Circle(radius) => math.Pi * radius * radius
case Rectangle(width, height) => width * height
case _ => 0.0
}
}
def main(args: Array[String]): Unit = {
// 1. 实例化圆形对象
val circle = Circle(2.0)
// 2. 实例化矩形对象
val rectangle = Rectangle(2.0, 3.0)
// 3. 调用求面积的方法
println(getArea(circle))
println(getArea(rectangle))
}
}
匹配for表达式
示例代码:
object day50 {
val m1 = Map(
"马云 - 阿里巴巴" -> 1964, "马化腾 - 腾讯" -> 1971,
"李彦宏 - 百度" -> 1968, "雷军 - 小米" -> 1969,
"丁磊 - 网易" -> 1971, "张一鸣 - 字节跳动" -> 1983,
"刘强东 - 京东" -> 1974, "程维 - 滴滴" -> 1983,
"王兴 - 美团" -> 1979, "周鸿祎 - 360" -> 1970,
"黄峥 - 拼多多" -> 1980, "李想 - 理想汽车" -> 1981,
"何小鹏 - 小鹏汽车" -> 1977, "贾跃亭 - 乐视" -> 1973,
"陈天桥 - 盛大网络" -> 1973, "周源 - 知乎" -> 1980,
"王小川 - 搜狗" -> 1978
)
// 请在m1这个Map,找出所有1983年出生的企业家
for((key,1983) <- m1){
println(key)
}
for((key,value) <- m1){
if(value == 1983)
println(key)
}
}