前言
本文是橙子出于兴趣爱好对Java官方教程的尝试翻译,几乎每日更新,感兴趣的朋友可以关注一下橙子;翻译过程中尽可能多的对一些关键词保留了英文原文,如果你想看最纯正的英文原版教材却又看不懂,可以试着来看一下橙子的翻译版啊,欢迎大家留言讨论,冲鸭!
更多相关文章点击阅读
Java官方教程目录2020最新版
流程控制语句 control flow statements
The switch Statement
与if-then和if-then-else语句不同,switch语句可以有多种可能的执行路径。switch适用于byte,short,char和int基本数据类型,以及枚举类型(enumerated types),String class和一些包装类(Character, Byte, Short, and Integer)。
下面代码示例SwitchDemo声明了一个名为month的int变量,其值代表一个月。代码使用switch语句根据mouth的值显示月份的名称。
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);
}
}
输出为:
August
switch语句的主体成为switch块。switch块中的语句可以用一个或多个case/default标签标记。switch语句计算其表达式,然后执行匹配case标签之后的所有语句。
也可以使用if-then-else语句来显示月份:
int month = 8;
if (month == 1) {
System.out.println("January");
} else if (month == 2) {
System.out.println("February");
}
... // and so on
根据可读性和判断条件和决定使用it-then-else语句还是switch语句。if-then-else语句可以基于值或条件(ranges of value or condition)的范围来判断,switch语句可以基于单个整形,枚举值或Stirng对象来判断。
另一个要点是break语句,每个break语句可以终止其外部的switch语句。控制流从switch块的第一条语句开始执行。break语句是必要的,因为没有它们的话,无论后续的case标签判断如何,switch块中匹配case标签之后的所有语句都会按顺序执行,直到遇到break语句为止。—即case穿透。下面程序SwitchDemoFallThrough展示了case穿透。程序显示了匹配月份及之后的所有月份。
public class SwitchDemoFallThrough {
public static void main(String[] args) {
java.util.ArrayList<String> futureMonths =
new java.util.ArrayList<String>();
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
从技术上说,不需要最后的break,因为已经跳出switch语句。但是建议加上break,以便于修改代码和减少错我。default部分处理case标签之外的所有情况。
一下代码示例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语句中使用String Using Strings in switch Statements
在Java SE 7及更高版本中,可以在switch语句中使用String对象。下面代码基于名为month的String值显示月份:
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表达式中的String与每个case标签中的表达式进行比较,就像使用String.equals方法一样。为了使代码示例可以忽略月份的大小写,将month用toLowerCase方法转换为小写,并且所有标签也用小写。
注意:示例中检查switch语句中的表达式是否为null。确保任何switch语句中的表达式都不为null,以防止引发NullPointerException。
想系统学习Java的朋友关注一下橙子,相信会有意想不到的收获鸭!