blob: c5561c6591c9ffbe5e05b71d23e2fe26ae9c0004 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
public class PrimitivePatternsSwitch1 {
public static void main(String[] argv) {
System.out.println(fn(new Number(1)));
System.out.println(fn(new Number(2)));
System.out.println(fn(new Number(140)));
System.out.println(fn(new Number(10040)));
}
static String fn(Number n) {
return switch (n.value()) {
case 1 -> "one";
case 2 -> "two";
case int i when (i >= 100 && i <1000) -> "many";
case int i -> "lots";
};
}
}
class Number {
private int i;
Number(int n) { this.i = n; }
int value() { return i; }
}
|