// ------------------------ Scala pattern matching match case ------------------- --------------- / * * * switch case is similar in Java, java switch case only the values match, but the match case scala can be judged to be below the range of wider * while the other differences and the java switch case is, java switch case of need to use break to stop, but as long as there is a scala branch to meet and deal with the case, it will not continue to judge a case at the branch * 1. the value of matching * 2 type matching * 3 and Array of list element matching * 4 of case class matches * 5 to have no matching value * _ is underlined, represents all of the above does not satisfy how default processing * * case IF match in a more detailed classification with one case * If you need to get this _ for specific values _ in the case, it is necessary to underline, is replaced with a variable name, this time you can get the value of the pattern matching syntax match will be assigned to the variable and then the variable name after drinking Case * otherwise we do not know What are the specific values that correspond _ * * Option: Some (a) represents the constant has a value, None indicates no value * / // values match DEF judgeScore (Grade: String): Unit = { Grade match { Case " A " => the printf ( " Excellent " ) Case " B " => the printf ( " Good " ) Case " C " => the printf ( " the Just SO SO " ) Case _ => the printf ( " you need Work Hard ") } } //Value matching increased if the guard double filter DEF judgeScore1 (Grade: String, name: String): Unit = { Grade match { Case " A " => the printf ( " Excellent " ) Case " B " => the printf ( " Good " ) Case " C " => printf ( " the Just SO SO " ) Case _ IF name == " ZM " => printf (name + " you are Good ") case= _> The printf ( " you need Work Hard " ) } } // judgeScore1 ( "F.", "ZM1") Import java.io._ // type matching case variables: Type => code pattern def processException (e : Exception): Unit = { E match { Case E1: an IllegalArgumentException => the printf ( " The Exception IS an IllegalArgumentException: " + E1) Case E2: a FileNotFoundException => the printf ( " The Exception IS a FileNotFoundException: " + E2) Case E3: IOException => printf ( "Exception IS IOException The: " + E3) Case _: Exception => the printf ( " The excepion happended OTHER " ) } // the specified array of the number of hits matching Array array matches a matching array elements beginning with the specified array element this effect is not simulated DEF greetArray (ARR: the array [String]): Unit = { ARR match { Case the array ( " Leo " ) => the printf ( " Hello, Leo " ) Case the array (NAME1, NAME2, NAME3 ) => the printf ( " The name IS people " + + NAME1 " " + name2 + " " + NAME3) Case the Array ( " Leo " , _ *) => the printf ( " Hi, Leo, Tell Me you Friends " ) Case _ => Print ( " Hi, How are you " ) } } greetArray (the Array ( " 1 " , " 2 " , " 3 " )) // for arrays call does not simulated // Scala in option and pattern matching / * * * option one is only two values Some expressed value, one is None It means no value * Scala option used for pattern matching value is not expressed in the value of even more compact than Null * / var personGrade the Map = ( " ZM " -> " 13 is " , " Liang " -> " . 7 " ) var Grade = personGrade . GET ( " ZM " ). GET Print (Grade) the println ( 12345 ) DEF getGrade (name: String): Unit = { var Grade = personGrade. GET (name) Grade match { Case Some(grade) => print("you grade is " + grade) case None => print("you are not here ") } } getGrade("zm2") } object Test { def main(args: Array[String]): Unit = { var personGrade = Map("zm"->"13","liang"->"7") //println(personGrade) // Print (personGrade.get ( "ZM") GET.) DEF getGrade (name: String): Unit ={ var . Grade = personGrade GET (name) Grade match { Case s Some (Grade) => Print ( " you Grade IS " + Grade) Case None => Print ( " you are here Wallpaper Not " ) } } // getGrade (" ZM ") // results Grade iS 13 is you // to match the specified array array array to a matching number of matching this effect matching array of array elements beginning with the specified element is not simulated DEF greetArray (ARR: the array [String]): Unit = { ARR match { Case the Array ( " Leo " ) => the printf ( "hello, Leo ") case Array(name1,name2,name3) => printf("the people name is" + name1 + " " + name2 + " " + name3) case Array("Leo",_*) => printf("hi,Leo, tell me you friends") case _ => print("hi,how are you") } } greetArray(Array("LEO " )) // for arrays call does not simulated } }