Java中 Enum 和 Switch 关键字的使用
一个 枚举是Java中一种独特的数据类型,它通常是常量的集合(集合) 。更具体地说, Java Enum 类型是一种独特的Java类。 Enum 可以保存常量、方法等。 Enum 关键字可以与 if 语句、switch 语句和迭代等一起使用。
例子:
Java
// Java Program to show working of Enum
// Keyword when declared outside the Class
enum Days {
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY;
}
public class temp {
// Driver method
public static void main(String[] args)
{
Days x = Days.FRIDAY;
System.out.println(x);
}
}
Java
// Java Program to show working
// of Switch statement
public class Example_Switch {
public static void main(String[] args)
{
// Declare a variable for switch statement
int num = 50;
// Switch keyword
switch (num) {
// Case statements
case 10:
System.out.println("10");
break;
case 20:
System.out.println("20");
break;
case 30:
System.out.println("30");
break;
// Default case statement
default:
System.out.println("Other than 10, 20 or 30");
}
}
}
Java
// Java Program to show the
// working of Enum keyword
// with Switch statement
// Enum keyword declared outside main class
enum Cars {
BMW,
JEEP,
AUDI,
VOLKSWAGON,
NANO,
FIAT;
}
// Main class
public class Main {
public static void main(String args[])
{
// Declaring Enum variable
Cars c;
c = Cars.AUDI;
// Switch keyword
switch (c) {
// Case statements
case BMW:
System.out.println("You choose BMW !");
break;
case JEEP:
System.out.println("You choose JEEP !");
break;
case AUDI:
System.out.println("You choose AUDI !");
break;
case VOLKSWAGON:
System.out.println("You choose VOLKSWAGON !");
break;
case NANO:
System.out.println("You choose NANO !");
break;
case FIAT:
System.out.println("You choose FIAT !");
default:
System.out.println("NEW BRAND'S CAR.");
break;
}
}
}
Java
// Java Program to Show
// working of Enum keyword
// with Switch statement
// Main class
public class MainClass {
// Declaring Enum keyword
// inside main class
enum Webseries {
GOT,
Breakingbad,
Lucifer,
Boys,
Mirzapur,
Moneyheist;
}
public static void main(String[] args)
{
// Declaring and Assigning choice to variable 'wb'
Webseries wb = Webseries.Mirzapur;
// Switch Keyword
switch (wb) {
// Case statements
case GOT:
System.out.println("Game of Thrones selected");
break;
case Breakingbad:
System.out.println("Breaking Bad selected");
break;
case Lucifer:
System.out.println("Lucifer selected");
break;
case Boys:
System.out.println("Boys selected");
break;
case Mirzapur:
System.out.println("Mirzapur selected");
break;
case Moneyheist:
System.out.println("Money Heist selected");
break;
default:
System.out.println("You are outdated !!!");
break;
}
}
}
输出:
FRIDAY
切换关键字
当用户有多个选择并希望为每个选择执行不同的任务时, Switch语句很有用。 Switch 语句允许根据值列表测试变量是否相等。每个值都称为一个案例。 switch Case 语句通常与break 语句一起使用,但它是可选的。
例子:
Java
// Java Program to show working
// of Switch statement
public class Example_Switch {
public static void main(String[] args)
{
// Declare a variable for switch statement
int num = 50;
// Switch keyword
switch (num) {
// Case statements
case 10:
System.out.println("10");
break;
case 20:
System.out.println("20");
break;
case 30:
System.out.println("30");
break;
// Default case statement
default:
System.out.println("Other than 10, 20 or 30");
}
}
}
输出:
Other than 10, 20 or 30
We can use also use Enum keyword with Switch statement. We can use Enum in Switch case statement in Java like int primitive. Below are some examples to show working of Enum with Switch statement.
示例 1:当 Enum 在主类之外时使用 Enum 和 Switch 语句
Java
// Java Program to show the
// working of Enum keyword
// with Switch statement
// Enum keyword declared outside main class
enum Cars {
BMW,
JEEP,
AUDI,
VOLKSWAGON,
NANO,
FIAT;
}
// Main class
public class Main {
public static void main(String args[])
{
// Declaring Enum variable
Cars c;
c = Cars.AUDI;
// Switch keyword
switch (c) {
// Case statements
case BMW:
System.out.println("You choose BMW !");
break;
case JEEP:
System.out.println("You choose JEEP !");
break;
case AUDI:
System.out.println("You choose AUDI !");
break;
case VOLKSWAGON:
System.out.println("You choose VOLKSWAGON !");
break;
case NANO:
System.out.println("You choose NANO !");
break;
case FIAT:
System.out.println("You choose FIAT !");
default:
System.out.println("NEW BRAND'S CAR.");
break;
}
}
}
输出:
You choose AUDI !
在上面的例子中,我们展示了当 Enum 在主类之外声明时, Enum 关键字如何与 Switch case 语句一起工作。
示例 2:当 Enum 在主类中时,将 Enum 与 Switch 语句一起使用
Java
// Java Program to Show
// working of Enum keyword
// with Switch statement
// Main class
public class MainClass {
// Declaring Enum keyword
// inside main class
enum Webseries {
GOT,
Breakingbad,
Lucifer,
Boys,
Mirzapur,
Moneyheist;
}
public static void main(String[] args)
{
// Declaring and Assigning choice to variable 'wb'
Webseries wb = Webseries.Mirzapur;
// Switch Keyword
switch (wb) {
// Case statements
case GOT:
System.out.println("Game of Thrones selected");
break;
case Breakingbad:
System.out.println("Breaking Bad selected");
break;
case Lucifer:
System.out.println("Lucifer selected");
break;
case Boys:
System.out.println("Boys selected");
break;
case Mirzapur:
System.out.println("Mirzapur selected");
break;
case Moneyheist:
System.out.println("Money Heist selected");
break;
default:
System.out.println("You are outdated !!!");
break;
}
}
}
输出:
Mirzapur selected
在上面的例子中,我们展示了当 Enum 在主类中声明时 Enum 关键字如何与 Switch case 语句一起工作。