## 1.多項選擇運算符：`switch`

Java 有另一個有趣的運算符，它繼承自它的祖父 (C++)。我們正在談論`switch`聲明。我們也可以稱其為多項選擇運算符。看起來有點麻煩：

``````switch(expression)
{
case value1: code1;
case value2: code2;
case value3: code3;
}``````

``````int temperature = 38;

switch(temperature)
{
case 36: System.out.println("Low");
case 37: System.out.println("Normal");
case 38: System.out.println("High");
}``````
``High``

## 2.`break`聲明在`switch`

``````int temperature = 36;

switch(temperature)
{
case 36: System.out.println("Low");
case 37: System.out.println("Normal");
case 38: System.out.println("High");
}``````
``````Low
Normal
High``````

``````int temperature = 36;

switch(temperature)
{
case 36:
System.out.println("Low");
break;
case 37:
System.out.println("Normal");
break;
case 38:
System.out.println("High");
}``````
``Low``

## 3.默認動作：`default`

`switch`如果沒有找到匹配的 case，則跳過語句的其餘部分，程序將在大括號結束`switch`語句後繼續執行。

`case`如果塊中沒有一個s`switch`與表達式的值匹配並且`switch``default`塊，則將執行 default 塊。例子：

``````int temperature = 40;
switch(temperature)
{
case 36:
System.out.println("Low");
break;
case 37:
System.out.println("Normal");
break;
case 38:
System.out.println("High");
break;
default:
System.out.println("Call an ambulance");
}``````
``Call an ambulance``

## 4.比較`switch`和`if-else`

``````int temperature = 40;
switch(temperature)
{
case 36:
System.out.println("Low");
break;
case 37:
System.out.println("Normal");
break;
case 38:
System.out.println("High");
break;
default:
System.out.println("Call an ambulance");
}``````
``````int temperature = 40;

if (temperature == 36)
{
System.out.println("Low");
}
else if (temperature == 37)
{
System.out.println("Normal");
}
else if (temperature == 38)
{
System.out.println("High");
}
else
{
System.out.println("Call an ambulance");
}``````

## 5. 語句中可以使用哪些表達式`switch`？

• 整數類型：`byte`, `short`, `int`,`long`
• `char`
• `String`
• 任何`enum`類型

``````Day day = Day.MONDAY;
switch (day)
{
case MONDAY:
System.out.println("Monday");
break;
case TUESDAY:
System.out.println("Tuesday");
break;
case WEDNESDAY:
System.out.println("Wednesday");
break;
case THURSDAY:
System.out.println("Thursday");
break;
case FRIDAY:
System.out.println("Friday");
break;
case SATURDAY:
System.out.println("Saturday");
break;
case SUNDAY:
System.out.println("Sunday");
break;
}``````

``````int temperature = 38;

switch(temperature)
{
case 36: System.out.println("Low");
case 37: System.out.println("Normal");
case 38: System.out.println("High");
}``````
``High``

## 2.`break`聲明在`switch`

``````int temperature = 36;

switch(temperature)
{
case 36: System.out.println("Low");
case 37: System.out.println("Normal");
case 38: System.out.println("High");
}``````
``````Low
Normal
High``````

``````int temperature = 36;

switch(temperature)
{
case 36:
System.out.println("Low");
break;
case 37:
System.out.println("Normal");
break;
case 38:
System.out.println("High");
}``````
``Low``

## 3.默認動作：`default`

`switch`如果沒有找到匹配的 case，則跳過語句的其餘部分，程序將在大括號結束`switch`語句後繼續執行。

`case`如果塊中沒有一個s`switch`與表達式的值匹配並且`switch``default`塊，則將執行 default 塊。例子：

``````int temperature = 40;
switch(temperature)
{
case 36:
System.out.println("Low");
break;
case 37:
System.out.println("Normal");
break;
case 38:
System.out.println("High");
break;
default:
System.out.println("Call an ambulance");
}``````
``Call an ambulance``

## 4.比較`switch`和`if-else`

``````int temperature = 40;
switch(temperature)
{
case 36:
System.out.println("Low");
break;
case 37:
System.out.println("Normal");
break;
case 38:
System.out.println("High");
break;
default:
System.out.println("Call an ambulance");
}``````
``````int temperature = 40;

if (temperature == 36)
{
System.out.println("Low");
}
else if (temperature == 37)
{
System.out.println("Normal");
}
else if (temperature == 38)
{
System.out.println("High");
}
else
{
System.out.println("Call an ambulance");
}``````

## 5. 語句中可以使用哪些表達式`switch`？

• 整數類型：`byte`, `short`, `int`,`long`
• `char`
• `String`
• 任何`enum`類型

``````Day day = Day.MONDAY;
switch (day)
{
case MONDAY:
System.out.println("Monday");
break;
case TUESDAY:
System.out.println("Tuesday");
break;
case WEDNESDAY:
System.out.println("Wednesday");
break;
case THURSDAY:
System.out.println("Thursday");
break;
case FRIDAY:
System.out.println("Friday");
break;
case SATURDAY:
System.out.println("Saturday");
break;
case SUNDAY:
System.out.println("Sunday");
break;
}``````