Black monkey house: Variable Scala mode

If in case keyword followed by the variable name, then the value of the expression before the match that will be assigned to the variable

for(i <- "+-*/3"){
  var sn = 0
  var dit = 0
  i match{
    case '+' => sn = 1
    case '-' => sn = 2
    //在 case 中,如果引入变量,满足条件的值,会返回给这个变量
    case ac if i.toString.equals("3") => sn = 5; dit = 4
      //打印的时候会自动调用 toString方法
      println("ac" + " -> " + ac)
    case _ => sn = 6
  }
    println(i + " " + sn + " " + dit)
}

Reproduced in: https: //www.jianshu.com/p/896d43fa9bcd

Guess you like

Origin blog.csdn.net/weixin_33953384/article/details/91182453