一、switch语句的使用
而如果再和if-then-else语句,switch语句可以有多个可能的执行路径。开关与字节,短,char,int和
原始数据类型。它还与枚举类型(在枚举类型的讨论),string的类,以及一些特殊的类包一定的原始
类型:字符,字节,短,和整数(在数字和字符串的讨论)。
下面的代码示例,switchdemo,声明一个int的值代表一个名叫月。该代码显示每个月的名称,根据月值
,使用switch语句。
public class SwitchDemo {
public static void main(String[] args) {
int month = 8;
String monthString;
switch (month) {
case 1: monthString = "January";
break;
case 2: monthString = "February";
break;
case 3: monthString = "March";
break;
case 4: monthString = "April";
break;
case 5: monthString = "May";
break;
case 6: monthString = "June";
break;
case 7: monthString = "July";
break;
case 8: monthString = "August";
break;
case 9: monthString = "September";
break;
case 10: monthString = "October";
break;
case 11: monthString = "November";
break;
case 12: monthString = "December";
break;
default: monthString = "Invalid month";
break;
}
System.out.println(monthString);
}
}
在这种情况下,八月打印到标准输出。
switch语句体就是一个开关块。在开关语句块可以标记与一个或多个案例或默认标签。switch语句对其
表达,然后执行的所有语句,遵循匹配的case标号。
你也可以显示每个月的名称与if-then-else语句:
nt month = 8;
if (month == 1) {
System.out.println("January");
} else if (month == 2) {
System.out.println("February");
}
... // 等等
决定是否使用if-then-else语句或语句是基于可读性,语句的表达式的测试。一个if-then-else语句可
以测试基于值的范围或条件表达式,而开关语句测试表达式仅基于一个整数,枚举值,或字符串对象。
另一个兴趣点是break语句。每一个break语句终止封闭开关语句。控制流继续下面的开关块的第一个语
句。break语句是必要的因为没有他们,在开关块语句失败:所有报表匹配的case标号后依次执行,无论
后续case标签的表达,直到遇到break语句是。程序switchdemofallthrough显示报表中的一个开关块落
空。该程序显示对应的整数月和月,按照年月:
public class SwitchDemoFallThrough {
public static void main(String[] args) {
java.util.ArrayList futureMonths =
new java.util.ArrayList();
int month = 8;
switch (month) {
case 1: futureMonths.add("January");
case 2: futureMonths.add("February");
case 3: futureMonths.add("March");
case 4: futureMonths.add("April");
case 5: futureMonths.add("May");
case 6: futureMonths.add("June");
case 7: futureMonths.add("July");
case 8: futureMonths.add("August");
case 9: futureMonths.add("September");
case 10: futureMonths.add("October");
case 11: futureMonths.add("November");
case 12: futureMonths.add("December");
break;
default: break;
}
if (futureMonths.isEmpty()) {
System.out.println("Invalid month number");
} else {
for (String monthName : futureMonths) {
System.out.println(monthName);
}
}
}
}
这是从代码的输出:
August
September
October
November
December
从技术上讲,最后的突破是不需要因为流瀑布从switch语句。利用休息的建议,修改代码更容易,更不
容易出错。默认的部分处理所有的值都没有一个明确的情况下部分处理。
下面的代码示例,switchdemo2,显示如何声明可以有多个case标签。该代码示例计算在一个特定的天数
:
class SwitchDemo2 {
public static void main(String[] args) {
int month = 2;
int year = 2000;
int numDays = 0;
switch (month) {
case 1: case 3: case 5:
case 7: case 8: case 10:
case 12:
numDays = 31;
break;
case 4: case 6:
case 9: case 11:
numDays = 30;
break;
case 2:
if (((year % 4 == 0) &&
!(year % 100 == 0))
|| (year % 400 == 0))
numDays = 29;
else
numDays = 28;
break;
default:
System.out.println("Invalid month.");
break;
}
System.out.println("Number of Days = "
+ numDays);
}
}
这是从代码的输出:
Number of Days = 29
二、在switch语句中使用字符串
在Java SE 7和之后,你可以在switch语句中的表达式中使用一个字符串对象。下面的代码示例,
stringswitchdemo,展示了基于字符串的值为月的月数:
public class StringSwitchDemo {
public static int getMonthNumber(String month) {
int monthNumber = 0;
if (month == null) {
return monthNumber;
}
switch (month.toLowerCase()) {
case "january":
monthNumber = 1;
break;
case "february":
monthNumber = 2;
break;
case "march":
monthNumber = 3;
break;
case "april":
monthNumber = 4;
break;
case "may":
monthNumber = 5;
break;
case "june":
monthNumber = 6;
break;
case "july":
monthNumber = 7;
break;
case "august":
monthNumber = 8;
break;
case "september":
monthNumber = 9;
break;
case "october":
monthNumber = 10;
break;
case "november":
monthNumber = 11;
break;
case "december":
monthNumber = 12;
break;
default:
monthNumber = 0;
break;
}
return monthNumber;
}
public static void main(String[] args) {
String month = "August";
int returnedMonthNumber =
StringSwitchDemo.getMonthNumber(month);
if (returnedMonthNumber == 0) {
System.out.println("Invalid month");
} else {
System.out.println(returnedMonthNumber);
}
}
}
从这个代码的输出是 8.
switch表达式中的字符串与每个case标签相关的如string.equals方法被用来表达的比较。为了
stringswitchdemo例接受任何月姑,月转换为小写(与toLowerCase方法),以及所有与案件相关的字符
串小写标签。
注:此示例检查在switch语句的表达是无效的。确保在任何开关语句的表达式是无效的防止一个
NullPointerException异常被抛出。