import java.util.Scanner;
public class Main {
enum TestEnum {
One(1), Two(3), Three(3);
private int code;
TestEnum(int code) {
this.code = code;
}
}
public static void main(String[] args) {
int n = new Scanner(System.in).nextInt();
System.out.println("n in TestEnum?");
}
}
import java.util.Arrays;
import java.util.Scanner;
public class Main {
enum TestEnum {
One(1), Two(2), Three(3);
private int code;
TestEnum(int code) {
this.code = code;
}
}
public static void main(String[] args) {
int n = new Scanner(System.in).nextInt();
boolean isPresent = Arrays.stream(TestEnum.values()).anyMatch(element -> element.code == n);
System.out.println("n in TestEnum? " + isPresent);
}
}
Enum
import java.lang.Enum;
import java.lang.IllegalArgumentException;
public class EvalEnum {
enum MyEnum {
One(1), Two(2), Three(3);
private int code;
MyEnum(int code) {
this.code = code;
}
}
public static void main(String[] args) {
System.out.println(
String.format("%s in MyEnum? %b", args[0], isPresent(args[0])));
}
private static boolean isPresent(String data) {
try {
Enum.valueOf(MyEnum.class, data);
return true;
} catch (IllegalArgumentException e) {
return false;
}
}
}
java EvalEnum One
One in MyEnum? true
java EvalEnum Zero
Zero in MyEnum? false