zoukankan      html  css  js  c++  java
  • Scala-模式匹配

    1、模式匹配 match case

    object controllAbstract {
      def main(args: Array[String]): Unit = {
        val x:Any="123";
        x match {
          case a:Int=> println("++++");
          case b:String=>println("----");
          case _ =>print(".....other type");
        }
      }
    }
    

    2、数组匹配

    
        val array = Array(1,2,0,3,4);
        array match {
          case Array(0)=>print("有0");
          case Array(a,b)=>print("含有两个元素");
          case Array(0 ,_*)=>print("从0开始");
          case _ =>print("other");
    
        }

    3、成绩评定采用模式匹配

    object controllAbstract {
      def main(args: Array[String]): Unit = {
    
        val result =  judgeGrade("A");
        print(result);
      }
    
      def judgeGrade (grade:String): Unit ={ 
        grade match{
          case "A" =>print("excellent");
          case "B"=>print("Good");
          case "C"=>print("Just so so");
          case "D"=>print("fail");
        }
      }
    }
    
    欢迎关注我的公众号:小秋的博客 CSDN博客:https://blog.csdn.net/xiaoqiu_cr github:https://github.com/crr121 联系邮箱:rongchen633@gmail.com 有什么问题可以给我留言噢~
  • 相关阅读:
    安利博客
    python 的高阶算法之堆排序
    functools模块用途
    类型注解
    高阶函数和装饰器
    生成器

    递归函数
    匿名函数
    函数 的 返回值作用域
  • 原文地址:https://www.cnblogs.com/flyingcr/p/10327091.html
Copyright © 2011-2022 走看看