-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pra5.scala
67 lines (56 loc) · 1.41 KB
/
Pra5.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.util.Locale
object Pra5 {
def main(args:Array[String]): Unit ={
// match 式
// Javaのswitch
val taro = "Taro"
var sex = taro match {
case "Taro" => "Male"
case "Jiro" => "Male"
case "Hanako" => "Female"
}
println(sex)
var one = 1
var num = one match {
case 1 => "one"
case 2 => "two"
// ワイルドカード
case _ => "other"
}
println(num)
// scala のmatch文にはフォールスルーが存在しない
// パターンマッチ
val lst = List("A", "B", "C")
var lstval = lst match {
case List("A", b, c) =>
println("b = " + b)
println("c = " + c)
case _ =>
println("Nothing")
}
// 中置きパターン
val lst2 = List("A", "B", "C")
lst2 match {
case "A" :: b :: c :: _ =>
println("b = " + b)
println("c = " + c)
case _ =>
println("Nothing")
}
// 型によるパターンマッチ
val obj: AnyRef = "String Literal"
obj match {
case v:java.lang.Integer =>
println("Integer!")
case v:String =>
println(v.toUpperCase(Locale.ENGLISH))
}
for (i <- 1 to 1000){
val s = new scala.util.Random(new java.security.SecureRandom()).
alphanumeric.take(5).toList match {
case List(a, b, c, d, _) => List(a, b, c, d, a)
}
println(s)
}
}
}